gpt4 book ai didi

python - 编写单独的 JSON 条目 Python 时遇到问题

转载 作者:行者123 更新时间:2023-11-28 18:40:48 25 4
gpt4 key购买 nike

希望有人能告诉我为什么会这样。我这里有三部电影,我正在尝试系统地获取电影的每个 JSON 数据并将它们打印到一个名为 data_fetch.txt 的单独文件中。我使用的网站是 omdbapi.com。我能够成功获取所有电影的 JSON,但出于某种奇怪的原因,在编写它时,它全部作为一个条目编写。因此,当我打开刚刚编写的文件并对条目进行计数时,计数器只会在应该有 3 个单独的条目时吐出 1 个条目。所以 3 个电影标题应该有 3 个 JSON。我想将每个 JSON 分成自己的条目的原因是因为我想获取它在 JSON 中的“流派”。另外,我不确定这是否与问题有关,但是当我将每个 JSON 条目打印到 python 解释器时,我收到此错误 ValueError: Extra data:

这是代码:

import urllib2, time, csv, json, re
def get_url(title):
base_url = 'http://www.omdbapi.com/?i=&t='
title_url = str(title)
total_url = base_url + title_url
return total_url

def get_json():
file = open('data_fetch.txt', 'wb')
lst = [['2010', 'Colin Firth', "The King's Speech "], ['2009', 'Jeff Bridges', 'Crazy Heart '], ['2008', 'Sean Penn', 'Milk ']]
for item in lst:
movie = item[2].strip()
if ' ' in movie:
title = movie.replace(' ', '%20')
else:
title = movie

reply = urllib2.urlopen(get_url(title))
html_doc = reply.read()
file.write(html_doc)
print html_doc

time.sleep(5)
file.close()
def counter():
file = open('data_fetch.txt', 'rU')
counter = 0
for entry in file:
counter+=1
return counter
file.close()

print counter() # this only prints 1

def json_data():
file = open('data_fetch.txt', 'rU')
for entry in file:
decoded = json.loads(entry)
print decoded
file.close()
jsond_data() #ValueError: Extra data:

希望有人可以帮助我,过去 3 个小时我一直在摆弄这个。我正在使用 Python 2.7。

更新:这是我的文件的内容:data_fetch.txt:

{"Title":"The King's Speech","Year":"2010","Rated":"R","Released":"25 Dec 2010","Runtime":"118 min","Genre": 《传记、戏剧、历史》、《导演》:《汤姆·霍珀》、《编剧》:《大卫·塞德勒(剧本)》、《 Actor 》:《科林·费尔斯、海伦娜·伯翰·卡特、德里克·雅各比、罗伯特·波特尔》、《剧情》 ":"大不列颠及北爱尔兰联合王国国王乔治六世的故事,他的即席即位以及帮助不确定的君主成为当之无愧的语言治疗师的故事。","Language":"English", "国家":"英国、美国、澳大利亚","Awards":"获得 4 项奥斯卡奖。另外 104 项获奖和 136 项提名。","海报":" http://ia.media-imdb.com/images/M/MV5BMzU5MjEwMTg2Nl5BMl5BanBnXkFtZTcwNzM3MTYxNA@@._V1_SX300.jpg","Metascore":"88","imdbRating ":"8.1","imdbVotes":"355,037","imdbID":"tt1504320","Type":"movie","Response":"True"}{"Title":"Crazy Heart","Year ":"2009","Rated":"R","Released":"05 Feb 2010","Runtime":"112 min","Genre":"Drama, Music, Romance","Director":"Scott Cooper","Writer":"Scott Cooper, Thomas Cobb (novel)","Actors":"Jeff Bridges, James Keane, Anna Felix, Paul Herman","Plot": “一位褪色的乡村音乐音乐家在一段注定失败的浪漫爱情中被迫重新评估他功能失调的生活,这也激励了他。”,“语言”:“英语,西类牙语”,“乡村”:“美国”,“奖项”:“赢得 2 项奥斯卡奖.另有 35 项胜利和 18 项提名。","海报":" http://ia.media-imdb.com/images/M/MV5BMTU0NDc5NjgzNl5BMl5BanBnXkFtZTcwNzc0NDIzMw@@._V1_SX300.jpg ","Metascore":"83","imdbRating":"7.3","imdbVotes":"55,547","imdbID":"tt1263670","Type":"movie","Response":"True"} {"Title":"Milk","Year":"2008","Rated":"R","Released":"30 Jan 2009","Runtime":"128 min","Genre":"传记、戏剧、历史”、“导演”:“格斯·范桑特”、“编剧”:“达斯汀·兰斯·布莱克”、“ Actor ”:“西恩·潘、埃米尔·赫希、乔什·布洛林、迭戈·卢纳”、“剧情”:“The哈维·米尔克 (Harvey Milk) 的故事,以及他作为美国同性恋事件家的奋斗历程,他为同性恋权利而战,并成为加州第一位公开的同性恋民选官员。","Language":"English","Country":"USA","Awards":"获得了 2 项奥斯卡奖。另有 62 项胜利和 95 项提名。","海报":" http://ia.media-imdb.com/images/M/MV5BMTI2OTM5NjUzMV5BMl5BanBnXkFtZTcwMzY1MTM5MQ@@._V1_SX300.jpg ","Metascore":"84","imdbRating":"7.7","imdbVotes":"114,699","imdbID":"tt1013753","Type":"movie","Response":"True"}

最佳答案

您必须在每行的末尾添加一个新行,否则所有内容都写在同一行中:

file.write(html_doc + '\n')

顺便说一下,对您的代码进行了一些改进:
import urllib2, time, csv, json, re

def get_url(title):
base_url = 'http://www.omdbapi.com/?i=&t='
title = title.strip().replace(' ', '%20')
return base_url + title


def get_json(lst, filename):
data = []
for year, actors, title in lst:
data.append(urllib2.urlopen(get_url(title)).read())
time.sleep(5)

# save data to file
with open(filename, 'wb') as f:
json.dump(data, f)


def json_data(filename):
with open(filename, 'rU') as f:
return json.load(f)


def counter(filename):
return len(json_data(filename))

lst = [['2010', 'Colin Firth', "The King's Speech "], ['2009', 'Jeff Bridges', 'Crazy Heart '], ['2008', 'Sean Penn', 'Milk ']]
filename = 'data_fetch.txt'
get_json(lst, filename)
print counter(filename)
print json_data(filename)

关于python - 编写单独的 JSON 条目 Python 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26350109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com