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"): # print(file_name, end="\n") 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: # 创建一个包含标题行的列表,以避免在处理某些键时出现“Index out of range”错误 data[key] = header if key == "[REF]" else [value] else: data[key].append(value) # 输出表头 #print(*header, sep="\t") print("file_name", *header, sep="\t") # 输出数据 for file_name in os.listdir("/public/work/Personal/wuxu/qiantao_17"): if file_name.endswith(".report"): print(file_name, end="\n") for i in range(len(data.get("[REF]", []))): row = [data.get(key, [""] * len(data.get("[REF]", [])))[i] for key in header] print(*row, sep="\t")修改此脚本,将file_name作为每行的第一列,其余每行每列为与其对应的row
时间: 2023-07-05 22:31:03 浏览: 118
修改后的代码如下:
```python
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")
```
修改后,每行的第一列为文件名,其余每列为对应的数据。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![exe](https://img-home.csdnimg.cn/images/20241231044909.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)