Как вывести список в файл? - PullRequest
0 голосов
/ 29 января 2020
from urllib import request
from redditscore.tokenizer import CrazyTokenizer
tokenizer = CrazyTokenizer()
url = "http://www.site.uottawa.ca/~diana/csi5386/A1_2020/microblog2011.txt"
for line in request.urlopen(url):
    tokens = tokenizer.tokenize(line.decode('utf-8'))
    print(tokens)
with open(r'C:\Users\dhyan\output.txt', 'w', encoding='utf-8', errors='replace') as f:
    for item in tokens:
        f.write("%s\n" % item)   

Это был мой код, который я пытаюсь запустить. Мой вывод находится в виде списка в «токенах». Однако я не смог записать это в текстовый файл.

Вот пример вывода моих данных:

['\ufeffsave', 'bbc', 'world', 'service', 'from', 'savage', 'cuts']
['a', 'lot', 'of', 'people', 'always', 'make', 'fun', 'about', 'the', 'end', 'of', 'the', 'world', 'but', 'the', 'question', 'is', '"are', 'u', 'ready', 'for', 'it']
['rethink', 'group', 'positive', 'in', 'outlook', 'technology', 'staffing', 'specialist', 'the', 'rethink', 'group', 'expects', 'revenues', 'to', 'be', 'marg']
['zombie', 'fund', 'manager', 'phoenix', 'appoints', 'new', 'ceo', 'phoenix', 'buys', 'up', 'funds', 'that', 'have', 'been', 'closed', 'to', 'new', 'business', 'and']
...