我想在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() 发布于 2015-04-04 09:59:22
pandas.DataFrame:
DataFrame是一种具有潜在不同类型列的二维标记数据结构.您可以把它看作是一个电子表格或SQL表,或者是一系列对象的一个片段。
您可以创建DataFrame对象,然后将其转换为字符串,并将该字符串转换为文件。
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')https://stackoverflow.com/questions/29444354
复制相似问题