json格式python

时间:2017-01-04 03:18:12

标签: python json unicode double-quotes

我有一个没有电影信息的数据集;我想从OMDBapi以​​json格式添加电影信息

我在python 3.5中编写这段代码为我这样做:

import urllib.request
import csv
import json
import datetime
from collections import defaultdict
from urllib import response
i=0
columns = defaultdict(list)
with open('C:\dataset\dataset.dat') as f:
  reader = csv.DictReader(f) 
  for row in reader:
    for (k,v) in row.items(): 
        columns[k].append(v)
with open('C:\dataset\dataset.dat','r',encoding='utf-8') as csvinput:
  with open('C:\dataset\dataset_edited.dat', 'w',encoding='utf-8') as csvoutput:
    writer = csv.writer(csvoutput)
    for row in csv.reader(csvinput):
        if row[0] == "user_id":
            writer.writerow(row+["movie_in_json_format"])
        else:
                movieJson=urllib.request.urlopen("http://www.omdbapi.com/?i=tt"+str(columns['item_id'][i])+"&y=&plot=short&r=json").read()
                movieJson=movieJson.decode('utf-8')
                writer.writerow(row+[movieJson])
                i=i+1

json格式以这种格式写入文件:

"{""Title"":""CitizenDog"",""Year"":""2004"",""Rated"":""N/A"",""Released"":""09 Mar 2006"",""Runtime"":""100 min"",""Genre"":""Comedy, Fantasy, Romance"",""Director"":""Wisit Sasanatieng"",""Writer"":""Koynuch (novel), Wisit Sasanatieng"",""Actors"":""Mahasamut Boonyaruk, Saengthong Gate-Uthong, Sawatwong Palakawong Na Autthaya, Nattha Wattanapaiboon"",""Plot"":""Pod is a man without a dream. He's a country bumpkin who comes to work at a tinned sardine factory in Bangkok. One day, Pod chops off his finger and packs it in the can, prompting him to go..."",""Language"":""Thai, English, Mandarin"",""Country"":""Thailand"",""Awards"":""2 wins & 1 nomination."",""Poster"":""http://ia.media-imdb.com/images/M/MV5BY2VlNDQwZTctMjBlNy00ZjYyLWEwYzAtNjA1YTNjNjVlMjU1XkEyXkFqcGdeQXVyMTIxMDUyOTI@._V1_SX300.jpg"",""Metascore"":""N/A"",""imdbRating"":""7.5"",""imdbVotes"":""1,544"",""imdbID"":""tt0444778"",""Type"":""movie"",""Response"":""True""}"

虽然应该是这样的:

{"Title":"Citizen Dog","Year":"2004","Rated":"N/A","Released":"09 Mar 2006","Runtime":"100 min","Genre":"Comedy, Fantasy, Romance","Director":"Wisit Sasanatieng","Writer":"Koynuch (novel), Wisit Sasanatieng","Actors":"Mahasamut Boonyaruk, Saengthong Gate-Uthong, Sawatwong Palakawong Na Autthaya, Nattha Wattanapaiboon","Plot":"Pod is a man without a dream. He's a country bumpkin who comes to work at a tinned sardine factory in Bangkok. One day, Pod chops off his finger and packs it in the can, prompting him to go...","Language":"Thai, English, Mandarin","Country":"Thailand","Awards":"2 wins & 1 nomination.","Poster":"http://ia.media-imdb.com/images/M/MV5BY2VlNDQwZTctMjBlNy00ZjYyLWEwYzAtNjA1YTNjNjVlMjU1XkEyXkFqcGdeQXVyMTIxMDUyOTI@._V1_SX300.jpg","Metascore":"N/A","imdbRating":"7.5","imdbVotes":"1,544","imdbID":"tt0444778","Type":"movie","Response":"True"}

我该怎么做才能以正确的格式在文件中写这个json?

〜注意" encoding =' utf-8'"由于此错误已添加到文件i / o:

'charmap' codec can't encode character '\xf3' in position 3152: character maps to <undefined>

3 个答案:

答案 0 :(得分:0)

问题通过以下代码解决:

import urllib.request
import csv
import datetime
from collections import defaultdict
from urllib import response
i=0
columns = defaultdict(list)

with open('C:\dataset\dataset.dat',encoding='utf-8') as f:
    reader = csv.DictReader(f) 
    for row in reader: 
        for (k,v) in row.items():  
            columns[k].append(v) 
with open('C:\dataset\dataset.dat','r',encoding='utf-8') as csvinput:
    f_writ = open('C:\dataset\dataset_edited.csv', 'w',encoding='utf-8')
    csvReader = csv.reader(csvinput)
    writer = csv.writer(f_writ, delimiter=',',
                lineterminator='\r\n',
                quotechar = "'"
                )
    for row in csvReader:
        if row[0] == "user_id":
            writer.writerow(row+["movie_in_json_format"])
        else:
            moviejson=urllib.request.urlopen("http://www.omdbapi.com/?i=tt"+str(columns['item_id'][i])+"&y=&plot=short&r=json").read()
            moviejson=moviejson.decode('utf-8')
            writer.writerow(row+[moviejson])
            i=i+1

答案 1 :(得分:0)

您能举例说明Dataset.dat吗?

答案 2 :(得分:-1)

如果没有其他工作,请强制删除多余的引号:

writer.writerow([field.strip('"') for field in row+[movieJson]])