总是收到这个错误
ValueError: invalid literal for int() with base 10: '3),'
从文本文件中读取看起来像这样:
[('cloud', 3),
('words', 2),
('code', 1),
('word', 1),
('appear', 1)]
如你所见,我试图用 word.replace() 替换一些东西
from pytagcloud import create_tag_image, make_tags
from pytagcloud.lang.counter import get_tag_counts
counts = []
with open("terms.txt") as FIN:
for line in FIN:
# Assume lines look like: word, number
word,n = line.strip().split()
word = word.replace(',', '')
word = word.replace("'", "")
word = word.replace("(", "")
word = word.replace("[", "")
word = word.replace(")", "")
word = word.replace(" ", "")
n = n.replace("'", "")
n = n.replace(" ", "")
counts.append([word,int(n.strip())])
tags = make_tags(counts, maxsize=120)
create_tag_image(tags, 'cloud_large.png', size=(1200, 800), fontname='Crimson Text')