如何在python中的每一个新行上拆分文件

时间:2015-04-04 03:21:36

标签: python

我有一个txt文件,我想根据新行拆分。我写代码然后在其中找到错误并帮助我。代码是

 file = open("C:/Python26/trigram.txt");
 f = open("Prefix.txt",'w');
 f_up = open("Prefix.txt",'w');
 contents=file.read();
 tokens = nltk.word_tokenize(contents);
 for t in tokens:
     print t

我的文件如下所示:

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

1 个答案:

答案 0 :(得分:2)

尝试这样:

with open("C:/Python26/trigram.txt") as f:
    my_list = f.readlines()
    for x in my_list:
        print x

OR

with open("C:/Python26/trigram.txt") as f:
    for lines in f:
        print lines