【发布时间】:2017-05-14 19:39:00
【问题描述】:
我看过一些以前的帖子,它们的解决方案对其他人有用,但由于某种原因对我不起作用。
我正在尝试编写一个 python 脚本来 1) 合并三个具有相同格式的文件,2) 仅删除重复的标题,3) 按 Specimen_ID 对行进行排序,以及 4) 在它们之间添加 2 个新的空白行每个唯一的Specimen_ID (即,每三行,除了第一个实例,由于标题需要是前 4 行)。
我有一部分脚本适用于前两个步骤和最后一步:
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")
对行排序有什么建议吗?此外,如果数据以制表符分隔的 txt 文件从 Excel 导出,我发现此脚本只会导致输出包含第一个 infile 的内容,而不会包含其他 infile 的内容。如果我只是将数据复制并粘贴到一个新的 txt 文件中并将它们用作 infiles,我没有问题。有谁知道我为什么会遇到这个问题?
输入文件文本示例(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
输入文件文本示例(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
【问题讨论】:
-
你能分享这三个文件的样本吗?您的代码看起来正确