[英]error in python: No JSON object could be decoded
我試圖在推文中找到最常用的詞。 我對 txt 文件進行了標記並將令牌傳遞到 json 文件中,但是當我執行 json.loads 時,它給了我一個錯誤:無法解碼 JSON 對象。
s_tweets.head()
print(s_tweets.iloc[:,2])
tweets = s_tweets.iloc[:,2]
#step 2: remove the special characters and punctuation
tlist = []
for t in tweets:
t_new=re.sub('[^A-Za-z0-9]+', ' ', t)
tlist.append(t_new)
#print(t_new)
#print(t_list)
test=word_tokenize(tlist[1])
print(test)
fname = 'tokensALL.json'
ff = open(fname, 'a')
for i in range(0,1751):
ff.write(str(word_tokenize(tlist[i])) + "\n")
ff.close()
###### find most frequent words
fname2 = 'tokensALL.json'
with open(fname2, 'r') as f:
count_all = Counter()
for line in f:
tweet = json.loads(line)
# Create a list with all the terms
terms_stop = [term for term in preprocess(tweet['text']) if
term not in stop]
# Update the counter
# terms_single = set(terms_all)
# Count hashtags only
terms_hash = [term for term in preprocess(tweet['text'])
if term.startswith('#')]
# Count terms only (no hashtags, no mentions)
terms_only = [term for term in preprocess(tweet['text'])
if term not in stop and
not term.startswith(('#', '@'))]
# mind the ((double brackets))
# startswith() takes a tuple (not a list) if
# we pass a list of inputs
terms_single = set(terms_stop)
terms_bigram = bigrams(terms_stop)
count_all.update(terms_stop)
# Print the first 5 most frequent words
print(count_all.most_common(5))
這是我的代碼和 json 文件內容示例(['cries', 'for', 'help', 'like', 'tears', 'in', 'rain'] ['rain', 'rain', 'go' , '離開']...等)
有人可以幫忙解決問題嗎? 謝謝!
您必須檢查您的 json 格式是否是有效的 json。 這是一篇討論所有可能性以及如何檢查它是否為有效 json 的帖子。 以前的帖子
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.