[英]how to write an output on multiple coloumns in python
我想將輸出寫在python文件的多列中。 我的代碼在兩行中生成輸出。 代碼是
f2 = open("C:/Python26/Semantics.txt",'w')
sem = ["cells", "gene","factor","alpha", "receptor", "t","promoter"]
with open("C:/Python26/trigram.txt") as f :
for x in f:
x = x.strip().split("$")
f2.write(" ".join(x) + " " + str(len(set(sem) & set(x)))+"\n")
f2.close()
我的文件如下所示:
IL-2$gene$expression$and
IL-2$gene$expression$and$NF-kappa
IL-2$gene$expression$and$NF-kappa$B
IL-2$gene$expression$and$NF-kappa$B$activation
gene$expression$and$NF-kappa$B$activation$through
expression$and$NF-kappa$B$activation$through$CD28
我目前的輸出
IL-2 gene expression and 1
IL-2 gene expression and NF-kappa 1
IL-2 gene expression and NF-kappa B 1
IL-2 gene expression and NF-kappa B activation 1
gene expression and NF-kappa B activation through 1
expression and NF-kappa B activation through CD28 0
我想要的輸出
Token cells gene factor……. promoter
IL-2 gene expression and 0 1 0 ……… 0
IL-2 gene expression and NF-kappa 0 1 0 ……… 0
IL-2 gene expression and NF-kappa B 0 1 0 ……… 0
IL-2 gene expression and NF-kappa B activation 0 1 0 ……… 0
gene expression and NF-kappa B activation through 0 1 0 ……… 0
expression and NF-kappa B activation through CD28 0 0 0 ……… 0
我認為我需要對代碼進行一些更改,以便通過嵌套循環來解決。 但是如何,我不知道。 我這樣做的代碼在下面不起作用
sem = ["cells", "b","expression", "cell", "gene","factor","activation","protein","activity","transcription","alpha","receptor","t","promotor","mrna","site","kinase","nfkappa","human"];
f2 = open("C:/Python26/Semantics.txt",'w')
with open("C:/Python26/trigram.txt") as file :
for s in sem:
for lines in file:
lines = lines.strip().split("$")
if s==lines:
f2.write(" ".join(lines) + "\t" +str(len(set(sem) & set(lines)))+"\n")
f2.write("\n")
f2.close()
DataFrame是二維標記的數據結構,具有可能不同類型的列。 您可以將其視為電子表格或SQL表或Series對象的字典。
您可以創建DataFrame對象,然后將其轉換為字符串並將該字符串write()
到您的文件中。
import pandas
col_labels = ['Token', 'cells', 'gene']
row_labels = ['x', 'y', 'z']
values_array = [[1, 2, 3],
[10, 20, 30],
[100, 200, 300]]
df = pandas.DataFrame(values_array, col_labels, row_labels)
print(df)
輸出 :
x y z
Token 1 2 3
cells 10 20 30
gene 100 200 300
要保存它,首先將對象轉換為字符串:
db_as_str = df.to_string()
with open('my_text_file.txt', 'w') as f:
f.write(db_as_str)
或按原樣保存在csv中:
db.to_csv('my_text_file.txt')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.