Python - 在特定情况下只读取文件的最后一行
Python - Only reading last line of file in specific circumstance
我正在尝试使用 Python 处理一些推文,并且我正在尝试对 7 条不同的推文中包含的最流行的词进行字数统计。我设置了我的文件,每条推文都是一个单独一行的 JSON 对象,当我尝试使用以下内容打印每条推文时,它完美地工作:
with open(fname, 'r') as f:
for line in f:
tweet = json.loads(line) # load it as Python dict
print(json.dumps(tweet, indent=4))
但是,当我尝试在我的字数统计中做类似的事情时,它要么读取文件的最后一行 7 次,要么只读取文件的最后一行一次。我正在使用以下代码,从结果中删除停用词:
with open(fname, 'r', encoding='utf8') as f:
count_all = Counter()
# Create a list with all the terms
terms_stop = [term for term in tokens if term not in stop]
for line in f:
# Update the counter
count_all.update(terms_stop)
# Print the first 5 most frequent words
print(count_all.most_common(5))
上面从最后一条推文中产生了 5 个随机词,每个词的计数为 7 - 这意味着它实质上阅读了最后一条推文 7 次,而不是阅读 7 条推文中的每一条。
以下代码旨在查看哪些词最常组合在一起。它从最后一条推文中生成 5 个随机分组的单词,计数仅为 1,这表示它只读取了最后一条推文(一次)和其他推文的 none。
with open(fname, 'r', encoding='utf8') as f:
count_all = Counter()
# Create a list with all the terms
terms_stop = [term for term in tokens if term not in stop]
# Import Bigrams to group words together
terms_bigram = bigrams(terms_stop)
for line in f:
# Update the counter
count_all.update(terms_bigram)
# Print the first 5 most frequent words
print(count_all.most_common(5))
我的json文件格式如下:
{"created_at":"Tue Oct 25 11:24:54 +0000 2016","id":4444444444,.....}
{"created_at":..... }
{etc}
非常感谢您的帮助!首先十分感谢。
更新:
不知道我是怎么错过的,但感谢大家的帮助!我忘了在我的 for 循环中包含 'line' 。这是工作代码:
with open(fname, 'r', encoding='utf8') as f:
count_all = Counter()
for line in f:
tweet = json.loads(line)
tokens = preprocess(tweet['text'])
# Create a list with all the terms
terms_stop = [term for term in tokens if term not in stop]
# Update the counter
count_all.update(terms_stop)
# Print the first 5 most frequent words
print(count_all.most_common(5))
我只需要将分词器与字数统计相结合。
试试这个来读取文件:
with open(fname) as d:
tweet = json.load(d)
如果这不起作用,post有关文件数据格式的更多信息。
新更新:
with open(fname) as d:
data = d.readlines()
tweet = [json.loads(x) for x in data]
这将为您提供字典列表(json 格式)
也许我遗漏了一些东西,但你永远不会在 for 循环中使用 line:
for line in f:
# Update the counter
count_all.update(terms_bigram)
所以你只是循环遍历每一行做同样的事情。
我正在尝试使用 Python 处理一些推文,并且我正在尝试对 7 条不同的推文中包含的最流行的词进行字数统计。我设置了我的文件,每条推文都是一个单独一行的 JSON 对象,当我尝试使用以下内容打印每条推文时,它完美地工作:
with open(fname, 'r') as f:
for line in f:
tweet = json.loads(line) # load it as Python dict
print(json.dumps(tweet, indent=4))
但是,当我尝试在我的字数统计中做类似的事情时,它要么读取文件的最后一行 7 次,要么只读取文件的最后一行一次。我正在使用以下代码,从结果中删除停用词:
with open(fname, 'r', encoding='utf8') as f:
count_all = Counter()
# Create a list with all the terms
terms_stop = [term for term in tokens if term not in stop]
for line in f:
# Update the counter
count_all.update(terms_stop)
# Print the first 5 most frequent words
print(count_all.most_common(5))
上面从最后一条推文中产生了 5 个随机词,每个词的计数为 7 - 这意味着它实质上阅读了最后一条推文 7 次,而不是阅读 7 条推文中的每一条。
以下代码旨在查看哪些词最常组合在一起。它从最后一条推文中生成 5 个随机分组的单词,计数仅为 1,这表示它只读取了最后一条推文(一次)和其他推文的 none。
with open(fname, 'r', encoding='utf8') as f:
count_all = Counter()
# Create a list with all the terms
terms_stop = [term for term in tokens if term not in stop]
# Import Bigrams to group words together
terms_bigram = bigrams(terms_stop)
for line in f:
# Update the counter
count_all.update(terms_bigram)
# Print the first 5 most frequent words
print(count_all.most_common(5))
我的json文件格式如下:
{"created_at":"Tue Oct 25 11:24:54 +0000 2016","id":4444444444,.....}
{"created_at":..... }
{etc}
非常感谢您的帮助!首先十分感谢。
更新: 不知道我是怎么错过的,但感谢大家的帮助!我忘了在我的 for 循环中包含 'line' 。这是工作代码:
with open(fname, 'r', encoding='utf8') as f:
count_all = Counter()
for line in f:
tweet = json.loads(line)
tokens = preprocess(tweet['text'])
# Create a list with all the terms
terms_stop = [term for term in tokens if term not in stop]
# Update the counter
count_all.update(terms_stop)
# Print the first 5 most frequent words
print(count_all.most_common(5))
我只需要将分词器与字数统计相结合。
试试这个来读取文件:
with open(fname) as d:
tweet = json.load(d)
如果这不起作用,post有关文件数据格式的更多信息。
新更新:
with open(fname) as d:
data = d.readlines()
tweet = [json.loads(x) for x in data]
这将为您提供字典列表(json 格式)
也许我遗漏了一些东西,但你永远不会在 for 循环中使用 line:
for line in f:
# Update the counter
count_all.update(terms_bigram)
所以你只是循环遍历每一行做同样的事情。