import os header = ["[REF]", "[QRY]", "[Sequences]", "TotalSeqs", "AlignedSeqs", "UnalignedSeqs", "[Bases]", "TotalBases","AlignedBases", "UnalignedBases", "[Alignments]", "1-to-1", "TotalLength", "AvgLength", "AvgIdentity","M-to-M", "TotalLength", "AvgLength", "AvgIdentity", "[Feature Estimates]", "Breakpoints", "Relocations", "Translocations", "Inversions", "Insertions", "InsertionSum", "InsertionAvg", "TandemIns", "TandemInsSum","TandemInsAvg", "[SNPs]", "TotalSNPs"] data = {} for file_name in os.listdir("/public/work/Personal/wuxu/qiantao_17"): if file_name.endswith(".report"): with open(os.path.join("/public/work/Personal/wuxu/qiantao_17", file_name), "r") as f: for line in f: split_line = line.strip().split() if len(split_line) < 2: continue key, value = split_line[:2] if key not in data: data[key] = header if key == "[REF]" else [value] else: data[key].append(value) # 输出表头 print("file_name", *header, sep="\t") # 输出数据 for file_name in os.listdir("/public/work/Personal/wuxu/qiantao_17"): if file_name.endswith(".report"): for i in range(len(data.get("[REF]", []))): row = [file_name] + [data.get(key, [""] * len(data.get("[REF]", [])))[i] for key in header] print(*row, sep="\t")修改上述代码,实现对file_name遍历,每行的第一列为每个文件名,其余每列为对应的数据
时间: 2023-07-05 11:31:09 浏览: 199
上述代码已经实现了对file_name遍历,每行的第一列为每个文件名,其余每列为对应的数据。如果您需要对代码进行修改,可以考虑将表头和数据分开输出,以便更好地查看和处理数据。具体实现可以参考下面的代码:
```
import os
# 定义表头和数据字典
header = ["[REF]", "[QRY]", "[Sequences]", "TotalSeqs", "AlignedSeqs", "UnalignedSeqs", "[Bases]", "TotalBases","AlignedBases", "UnalignedBases", "[Alignments]", "1-to-1", "TotalLength", "AvgLength", "AvgIdentity","M-to-M", "TotalLength", "AvgLength", "AvgIdentity", "[Feature Estimates]", "Breakpoints", "Relocations", "Translocations", "Inversions", "Insertions", "InsertionSum", "InsertionAvg", "TandemIns", "TandemInsSum","TandemInsAvg", "[SNPs]", "TotalSNPs"]
data = {}
# 读取文件夹中的“.report”文件,并将数据存储到字典中
for file_name in os.listdir("/public/work/Personal/wuxu/qiantao_17"):
if file_name.endswith(".report"):
with open(os.path.join("/public/work/Personal/wuxu/qiantao_17", file_name), "r") as f:
for line in f:
split_line = line.strip().split()
if len(split_line) < 2:
continue
key, value = split_line[:2]
if key not in data:
data[key] = header if key == "[REF]" else [value]
else:
data[key].append(value)
# 输出表头
print("file_name", *header, sep="\t")
# 输出数据
for file_name in os.listdir("/public/work/Personal/wuxu/qiantao_17"):
if file_name.endswith(".report"):
for i in range(len(data.get("[REF]", []))):
# 分别输出文件名和对应的数据
print(file_name, end="\t")
for key in header:
print(data.get(key, [""] * len(data.get("[REF]", [])))[i], end="\t")
print()
```
这样输出的结果中,每行的第一列为文件名,其余每列为对应的数据,更加清晰易懂。
阅读全文