【问题标题】:Writing function in Python saves only the last string (Python)在 Python 中编写函数只保存最后一个字符串(Python)
【发布时间】:2017-09-08 14:15:06
【问题描述】:

我正在使用 Python 中的“nltk”进行 pos 标记,当我打印下面的代码时,它工作得非常好。

import nltk 
import pos_tag
import nltk.tokenize 
import numpy

f = open(r'C:\Users\sample_data.txt')
data = f.readlines()

#Parse the text file for NER with POS Tagging
for line in data:
    tokens = nltk.word_tokenize(line)
    tagged = nltk.pos_tag(tokens)
    #print (tagged)

output = open(r"C:\Users\output3.csv", "w")
output.write(str(tagged))
f.close()

所以当我打印上面的代码时,输​​出看起来像这样,这正是我想要的。

[('This', 'DT'), ('is', 'VBZ'), ('a', 'DT'), ('simple', 'JJ'), ('sentence', 'NN')]
[('I', 'PRP'), ('love', 'VBP'), ('this', 'DT'), ('company', 'NN'), ('.', '.'), ('This', 'DT'), ('company', 'NN'), ('is', 'VBZ'), ('so', 'RB'), ('good', 'JJ'), ('.', '.')]
[('I', 'PRP'), ('am', 'VBP'), ('not', 'RB'), ('inovlved', 'VBN'), ('with', 'IN'), ('this', 'DT'), ('work', 'NN'), ('.', '.'), ('So', 'RB'), ('hard', 'JJ'), ('!', '.')]
[('What', 'WP'), ('are', 'VBP'), ('you', 'PRP'), ('doing', 'VBG'), ('?', '.'), ('Are', 'NNP'), ('you', 'PRP'), ('nut', 'RB'), ('?', '.')]
[('Can', 'MD'), ('I', 'PRP'), ('borrow', 'VB'), ('your', 'PRP$'), ('jar', 'NN'), ('?', '.'), ('Just', 'NNP'), ('for', 'IN'), ('today', 'NN'), ('.', '.')]

但是当我使用代码中的最后三行编写它时,它只保存文本文件中的最后一个字符串(即,[('Can','MD'),('I','PRP') , ('borrow', 'VB'), ('your', 'PRP$'), ('jar', 'NN'), ('?', '.'), ('Just', 'NNP' ), ('for', 'IN'), ('today', 'NN'), ('.', '.')])。

我想将整个结果保存在 txt 或 csv 文件中,每个 [ ] 中的一个字符串应保存在 txt 或 csv 文件中的一行中。

我对 Python 真的很陌生,因此我们将不胜感激。

【问题讨论】:

  • tagged = nltk.pos_tag(tokens) 重新定义数据中每一行的标签。到最后,您只保存最后一行。

标签: python string save pos-tagger


【解决方案1】:

您应该将每一行保存在一个列表中,然后写入整个列表:

tagged_list = []
#Parse the text file for NER with POS Tagging
for line in data:
    tokens = nltk.word_tokenize(line)
    tagged_list.append(str(nltk.pos_tag(tokens)))

output = open(r"C:\Users\output3.csv", "w")
output.write('\n'.join(tagged_list))
output.close()

tagged_list 中添加所有要写入的行。使用 '\n'.join(tagged) 编写它们,用 '\n' 分隔(即每个在不同的行中)

【讨论】:

  • 非常感谢!它完美地工作。只有一个小错误是在加入后立即在括号内写入 tagged_list 而不是 tagged。非常感谢!
【解决方案2】:

你有一个缩进错误。

import nltk 
import pos_tag
import nltk.tokenize 
import numpy

f = open(r'C:\Users\sample_data.txt')
data = f.readlines()

#Parse the text file for NER with POS Tagging
for line in data:
    tokens = nltk.word_tokenize(line)
    tagged = nltk.pos_tag(tokens)
    #print (tagged)

    output = open(r"C:\Users\output3.csv", "a")
    output.write(str(tagged)+'\n')
f.close()
output.close()

【讨论】:

    【解决方案3】:

    编辑:要回答您的原始问题,您需要在原始代码中循环调用output.write(str(tagged))

    即使其他答案确实回答了这个问题,我还是想建议对您的实施进行一些更改

    • 在处理资源时尽量使用with,因为它最终会自动关闭资源
    • 文件打开后,您可以简单地迭代 f 变量

    最终结果如下所示:

    import nltk
    
    # file will be closed once out of the scope
    with open(r'C:\Users\sample_data.txt') as f:  
        with open(r'C:\Users\output3.csv', 'w') as output:
            for line in f:
                tokens = nltk.word_tokenize(line)
                tagged = nltk.pos_tag(tokens)
                output.write(str(tagged)+'\n')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-06
      • 2012-02-14
      • 2022-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-11
      相关资源
      最近更新 更多