我编写了一个python程序,在其中将各种文件存储在目录中。这些文件的字符数为n。
代码读取这些文件并打印出
我得到以下输出:
getAbsoluteFile
我想以表格格式显示以上输出
请帮助
答案 0 :(得分:0)
假设您的O / P看起来像这样:
36.HomoSapiensHOXD10gene.fa 11753 ([2296, 2304, 5794, 10258, 11272],)
37.HomoSapiensHOXD11gene.fa 6139 ([],)
38.HomoSapiensHOXD13gene.fa 3648 ([403, 2718],)
首先在终端上运行程序,例如
python yourfile.py > yourfile.txt
这会将您的输出复制到yourfile.txt 然后使用以下python代码将其转换为整齐的表格格式:
import pandas as pd
file_data = open('yourfile.txt', 'r').read().splitlines() # Read your file
data = pd.DataFrame(columns=['file', 'length', 'pattern'])
file = [] # filennames
le = [] #length of file
pattern = [] # patterns
for f in file_data:
s = f.split(" ", 2) #Two times split on space to get a list of all three parameters
file_name = s[0].split('.', 1)[1] #extracting just the file name ignoring numbers
length = s[1]
patt = s[2].replace('(', '').replace('[', '').replace(']', '').replace(')', '') # Removing useless brackets
try:
pattern.append(patt.rsplit(',', 1)[0]) # Removing the comma at the end
except:
pattern.append('no pattern')
file.append(file_name)
le.append(length)
data['file'] = file
data['length'] = le
data['pattern'] = pattern
print(data)
现在,如果您要这样做,您将获得所需的输出。您还可以使用熊猫将其保存为excel / csv格式。
file length pattern
0 HomoSapiensHOXD10gene.fa 11753 2296, 2304, 5794, 10258, 11272
1 HomoSapiensHOXD11gene.fa 6139
2 HomoSapiensHOXD13gene.fa 3648 403, 2718
答案 1 :(得分:0)
请尝试使用prettytable
from prettytable import PrettyTable
x = PrettyTable()
x.field_names = ["Field Name", "Length", "Pattern"]
x.add_row(["HOXD10gene.fa", 11753, [2296, 2304, 5794, 10258, 11272]])
print(x)
您可以尝试如下从csv文件导入数据
from prettytable import from_csv
with open("data.csv", "r") as fp:
x = from_csv(fp)
print(x)