I've seen some previous posts that have solutions that work for others, but for some reason has not been working for me.
I'm trying to write a python script to 1) merge three files that have the same format, 2) remove duplicate headers only, 3) sorts the rows by Specimen_ID
, and 4) adds 2 new blank lines between every unique Specimen_ID
(i.e., every three lines, except first instance would need to be first 4 line due to headers).
I have part of a script that works for the first two and last steps:
import glob
read_files = glob.glob("*.txt")
header_saved = False
linecnt=0
with open("merged_data.txt", "wb") as outfile:
for f in read_files:
with open(f, "rb") as infile:
header = next(infile)
if not header_saved:
outfile.write(header)
header_saved = True
for line in infile:
outfile.write(line)
linecnt=linecnt+1
if (linecnt%3)==0:
outfile.write("\n\n")
Any suggestions on sorting the rows? Also, if data is exported out of Excel in tab-delimited txt files, I find that this script will only result in output containing the contents of the first infile, but not the others. If I just copy and paste the data into a new txt file and use these as infiles, I have no problems. Does anyone know why I'm experiencing this issue?
Example input file text (infile 1):
Specimen_ID Measured_by_initals Measure_date Sex Beak_length Pronotal_width Right_fore_femur_length Right_fore_femur_width Left_fore_femur_length Left_fore_femur_width Right_hind_femur_length Right_hind_femur_width Left_hind_femur_length Left_hind_femur_width Right_hind_femur_area Left_hind_femur_area Right_hind_tibia_width Left_hind_tibia_width Notes
a 1 30-Dec-16 M 4 4 4 4 4 4 4 4 4 4 4 4 4 4
b 1 30-Dec-16 F 4 4 4 4 4 4 4 4 4 4 4 4 4 4 beak bent
c 1 30-Dec-16 M 4 4 4 4 4 4 4 4 4 4 4 4 4 4
d 1 30-Dec-16 F 4 4 4 4 4 4 4 4 4 4 4 4 4 4
e 1 30-Dec-16 F 4 4 4 4 4 4 4 4 4 4 4 4 4 4 pronotum deformed
f 1 30-Dec-16 F 4 4 4 4 4 4 4 4 4 4 4 4 4 4
Example input file text (infile 2):
Specimen_ID Measured_by_initals Measure_date Sex Beak_length Pronotal_width Right_fore_femur_length Right_fore_femur_width Left_fore_femur_length Left_fore_femur_width Right_hind_femur_length Right_hind_femur_width Left_hind_femur_length Left_hind_femur_width Right_hind_femur_area Left_hind_femur_area Right_hind_tibia_width Left_hind_tibia_width Notes
a 2 30-Dec-16 M 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1
b 2 30-Dec-16 F 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1
c 2 30-Dec-16 M 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1
d 2 30-Dec-16 F 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1
e 2 30-Dec-16 F 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1
f 2 30-Dec-16 F 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.1