【问题标题】:how to print the output to a text file in python如何将输出打印到python中的文本文件
【发布时间】:2020-01-22 22:44:12
【问题描述】:
import nltk
from urllib import request

url = "http://www.site.uottawa.ca/~diana/csi5386/A1_2020/microblog2011.txt"
response = request.urlopen(url)
raw = response.read().decode('utf8')
tokens = nltk.word_tokenize(raw)
tokens

在上面的代码中,输出是列表形式的'tokens'。如何将此列表复制到文本文件中?

【问题讨论】:

标签: python-3.x nlp


【解决方案1】:

你需要做这样的事情:

with open('filename.txt', 'w') as f:
    for i in tokens:
        f.write(i)  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    • 2011-04-03
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    相关资源
    最近更新 更多