如何从字符串中读取JSON文档?

时间:2014-07-11 13:19:38

标签: python json

我可以从文件中读取JSON文档而不会出现问题。如果我将该文档放入字符串中则失败。

# Reading JSON
import json

### Opening a JSON document:

# document contains a series of blog posts in JSON format
document = open('tumblr_posts.json', 'r').read()

# load the JSON formatted document
blog_posts = json.loads(document)

# display the title of the blog to show that it opens correctly
print blog_posts['response']['blog']['url']

输出:

  

http://pitchersandpoets.tumblr.com/

现在我尝试从字符串中读取JSON:

# I copy + paste the JSON from the document (I enclose it in single quotes):
json_from_doc = '{"meta":{"status":200,"msg":"OK"},"response":{"blog":{"title":"Pitchrs & Poets","name":"pitchersandpoets","posts":1139,"url":"http:\/\/pitchersandpoets.tumblr.com\/","updated":1404743947,"description":"Baseball & Cultural Ephemera compiled in the tradition of Hiroshi Arakawa, Fernando Valenzuela, and Lance Berkman. Brought to you by \u003Ca href=\u0022http:\/\/pitchersandpoets.com\u0022\u003E\u003Cstrong\u003EPitchers & Poets\u003C\/a\u003E. \u003Ca href=\u0022http:\/\/twitter.com\/pitchersnpoets\u0022\u003E@pitchersnpoets\u003C\/strong\u003E\u003C\/a\u003E.","ask":true,"ask_page_title":"Questions","ask_anon":true,"is_nsfw":false,"share_likes":false},"posts":[{"blog_name":"pitchersandpoets","id":10413871154,"post_url":"http:\/\/pitchersandpoets.tumblr.com\/post\/10413871154\/findthosedetonators-enter-sandman","slug":"findthosedetonators-enter-sandman","type":"photo","date":"2011-09-19 20:58:03 GMT","timestamp":1316465883,"state":"published","format":"html","reblog_key":"SpQlLmh1","tags":["KHAKI DIEM","new york yankees","mariano rivera","baseball"],"short_url":"http:\/\/tmblr.co\/ZhSWUx9ijn0o","highlighted":[],"note_count":86,"caption":"\u003Cp\u003E\u003Ca href=\u0022http:\/\/findthosedetonators.tumblr.com\/post\/10413445586\u0022\u003Efindthosedetonators\u003C\/a\u003E:\u003C\/p\u003E\n\u003Cblockquote\u003E\n\u003Cp\u003EENTER SANDMAN\u003C\/p\u003E\n\u003C\/blockquote\u003E","image_permalink":"http:\/\/pitchersandpoets.tumblr.com\/image\/10413871154","photos":[{"caption":"","alt_sizes":[{"width":600,"height":833,"url":"http:\/\/38.media.tumblr.com\/tumblr_lrsf2vxPYU1qcw8igo1_1280.jpg"},{"width":500,"height":694,"url":"http:\/\/38.media.tumblr.com\/tumblr_lrsf2vxPYU1qcw8igo1_500.jpg"},{"width":400,"height":555,"url":"http:\/\/38.media.tumblr.com\/tumblr_lrsf2vxPYU1qcw8igo1_400.jpg"},{"width":250,"height":347,"url":"http:\/\/31.media.tumblr.com\/tumblr_lrsf2vxPYU1qcw8igo1_250.jpg"},{"width":100,"height":139,"url":"http:\/\/33.media.tumblr.com\/tumblr_lrsf2vxPYU1qcw8igo1_100.jpg"},{"width":75,"height":75,"url":"http:\/\/31.media.tumblr.com\/tumblr_lrsf2vxPYU1qcw8igo1_75sq.jpg"}],"original_size":{"width":600,"height":833,"url":"http:\/\/38.media.tumblr.com\/tumblr_lrsf2vxPYU1qcw8igo1_1280.jpg"}}]},{"blog_name":"pitchersandpoets","id":10190006450,"post_url":"http:\/\/pitchersandpoets.tumblr.com\/post\/10190006450\/1-for-glory-of-their-times-quote","slug":"1-for-glory-of-their-times-quote","type":"photo","date":"2011-09-14 02:49:52 GMT","timestamp":1315968592,"state":"published","format":"html","reblog_key":"3zAmAHdR","tags":["stan coveleski","pitcher","philadelphia athletics","cleveland indians","washington senators","new york yankees","1969"],"short_url":"http:\/\/tmblr.co\/ZhSWUx9VNoYo","highlighted":[],"note_count":28,"caption":"\u003Cp\u003E+1 for Glory of Their Times quote\u003C\/p\u003E","image_permalink":"http:\/\/pitchersandpoets.tumblr.com\/image\/10190006450","photos":[{"caption":"","alt_sizes":[{"width":1280,"height":1017,"url":"http:\/\/31.media.tumblr.com\/tumblr_lrhdkf4eWL1qg2jgqo1_1280.jpg"},{"width":500,"height":397,"url":"http:\/\/37.media.tumblr.com\/tumblr_lrhdkf4eWL1qg2jgqo1_500.jpg"},{"width":400,"height":318,"url":"http:\/\/37.media.tumblr.com\/tumblr_lrhdkf4eWL1qg2jgqo1_400.jpg"},{"width":250,"height":199,"url":"http:\/\/38.media.tumblr.com\/tumblr_lrhdkf4eWL1qg2jgqo1_250.jpg"},{"width":100,"height":79,"url":"http:\/\/38.media.tumblr.com\/tumblr_lrhdkf4eWL1qg2jgqo1_100.jpg"},{"width":75,"height":75,"url":"http:\/\/37.media.tumblr.com\/tumblr_lrhdkf4eWL1qg2jgqo1_75sq.jpg"}],"original_size":{"width":1280,"height":1017,"url":"http:\/\/31.media.tumblr.com\/tumblr_lrhdkf4eWL1qg2jgqo1_1280.jpg"}}]},{"blog_name":"pitchersandpoets","id":8907365173,"post_url":"http:\/\/pitchersandpoets.tumblr.com\/post\/8907365173\/via","slug":"via","type":"photo","date":"2011-08-14 14:27:05 GMT","timestamp":1313332025,"state":"published","format":"html","reblog_key":"CjSOphoO","tags":["yogi berra","comix","babe ruth","baseball","new york yankees"],"short_url":"http:\/\/tmblr.co\/ZhSWUx8Iwvir","highlighted":[],"note_count":11,"caption":"\u003Cp\u003E\u003Ca href=\u0022http:\/\/www.americanmemorabilia.com\/Auction_Item.asp?Auction_ID=66223&qf=people&q=Babe%20Ruth&s=&p=0&t=Open\u0022\u003Evia\u003C\/a\u003E\u003C\/p\u003E","image_permalink":"http:\/\/pitchersandpoets.tumblr.com\/image\/8907365173","photos":[{"caption":"","alt_sizes":[{"width":1140,"height":1650,"url":"http:\/\/31.media.tumblr.com\/tumblr_lpx9h5oMMm1qk9es5o1_1280.jpg"},{"width":500,"height":724,"url":"http:\/\/33.media.tumblr.com\/tumblr_lpx9h5oMMm1qk9es5o1_500.jpg"},{"width":400,"height":579,"url":"http:\/\/37.media.tumblr.com\/tumblr_lpx9h5oMMm1qk9es5o1_400.jpg"},{"width":250,"height":362,"url":"http:\/\/37.media.tumblr.com\/tumblr_lpx9h5oMMm1qk9es5o1_250.jpg"},{"width":100,"height":145,"url":"http:\/\/31.media.tumblr.com\/tumblr_lpx9h5oMMm1qk9es5o1_100.jpg"},{"width":75,"height":75,"url":"http:\/\/38.media.tumblr.com\/tumblr_lpx9h5oMMm1qk9es5o1_75sq.jpg"}],"original_size":{"width":1140,"height":1650,"url":"http:\/\/31.media.tumblr.com\/tumblr_lpx9h5oMMm1qk9es5o1_1280.jpg"}}]},{"blog_name":"pitchersandpoets","id":8444337350,"post_url":"http:\/\/pitchersandpoets.tumblr.com\/post\/8444337350\/happyhelmut-thurman-munson-by-paulzilla","slug":"happyhelmut-thurman-munson-by-paulzilla","type":"photo","date":"2011-08-03 22:16:56 GMT","timestamp":1312409816,"state":"published","format":"html","reblog_key":"jf22yjhF","tags":["baseball","new york yankees","thurman munson"],"short_url":"http:\/\/tmblr.co\/ZhSWUx7tKbp6","highlighted":[],"note_count":25,"source_url":"http:\/\/www.flickr.com\/photos\/paulwindle\/5611152786\/in\/photostream","source_title":"Flickr \/ paulwindle","caption":"\u003Cp\u003E\u003Ca href=\u0022http:\/\/happyhelmut.tumblr.com\/post\/8443779188\u0022\u003Ehappyhelmut\u003C\/a\u003E:\u003C\/p\u003E\n\u003Cblockquote\u003E\n\u003Cp\u003Ethurman munson (by \u003Ca href=\u0022http:\/\/www.flickr.com\/photos\/paulwindle\/5611152786\/in\/photostream\u0022\u003Epaulzilla\u003C\/a\u003E)\u003C\/p\u003E\n\u003C\/blockquote\u003E","link_url":"http:\/\/www.flickr.com\/photos\/paulwindle\/5611152786\/in\/photostream","image_permalink":"http:\/\/pitchersandpoets.tumblr.com\/image\/8444337350","photos":[{"caption":"","alt_sizes":[{"width":480,"height":500,"url":"http:\/\/33.media.tumblr.com\/tumblr_lpdhajQYL51qep2cso1_500.jpg"},{"width":400,"height":417,"url":"http:\/\/38.media.tumblr.com\/tumblr_lpdhajQYL51qep2cso1_400.jpg"},{"width":250,"height":260,"url":"http:\/\/38.media.tumblr.com\/tumblr_lpdhajQYL51qep2cso1_250.jpg"},{"width":100,"height":104,"url":"http:\/\/38.media.tumblr.com\/tumblr_lpdhajQYL51qep2cso1_100.jpg"},{"width":75,"height":75,"url":"http:\/\/37.media.tumblr.com\/tumblr_lpdhajQYL51qep2cso1_75sq.jpg"}],"original_size":{"width":480,"height":500,"url":"http:\/\/33.media.tumblr.com\/tumblr_lpdhajQYL51qep2cso1_500.jpg"}}]},{"blog_name":"pitchersandpoets","id":7346570590,"post_url":"http:\/\/pitchersandpoets.tumblr.com\/post\/7346570590\/i-sincerely-hope-no-one-tried-to-learn-to-swim","slug":"i-sincerely-hope-no-one-tried-to-learn-to-swim","type":"photo","date":"2011-07-07 17:06:05 GMT","timestamp":1310058365,"state":"published","format":"html","reblog_key":"KSKLutI5","tags":["babe ruth","New York Yankees","strength"],"short_url":"http:\/\/tmblr.co\/ZhSWUx6ruyLU","highlighted":[],"note_count":4,"caption":"\u003Cp\u003Ei sincerely hope no one tried to learn to swim based solely on this magazine’s advice\u003C\/p\u003E","image_permalink":"http:\/\/pitchersandpoets.tumblr.com\/image\/7346570590","photos":[{"caption":"","alt_sizes":[{"width":849,"height":1270,"url":"http:\/\/31.media.tumblr.com\/tumblr_lnqlcyTMWY1qk9es5o1_1280.jpg"},{"width":500,"height":748,"url":"http:\/\/38.media.tumblr.com\/tumblr_lnqlcyTMWY1qk9es5o1_500.jpg"},{"width":400,"height":598,"url":"http:\/\/31.media.tumblr.com\/tumblr_lnqlcyTMWY1qk9es5o1_400.jpg"},{"width":250,"height":374,"url":"http:\/\/31.media.tumblr.com\/tumblr_lnqlcyTMWY1qk9es5o1_250.jpg"},{"width":100,"height":150,"url":"http:\/\/38.media.tumblr.com\/tumblr_lnqlcyTMWY1qk9es5o1_100.jpg"},{"width":75,"height":75,"url":"http:\/\/37.media.tumblr.com\/tumblr_lnqlcyTMWY1qk9es5o1_75sq.jpg"}],"original_size":{"width":849,"height":1270,"url":"http:\/\/31.media.tumblr.com\/tumblr_lnqlcyTMWY1qk9es5o1_1280.jpg"}}]},{"blog_name":"pitchersandpoets","id":6510870769,"post_url":"http:\/\/pitchersandpoets.tumblr.com\/post\/6510870769\/last-one-for-tonight-its-pretty-good-mr","slug":"last-one-for-tonight-its-pretty-good-mr","type":"photo","date":"2011-06-14 03:32:58 GMT","timestamp":1308022378,"state":"published","format":"html","reblog_key":"gZfTDRxy","tags":["joe dimaggio","lamp","baseball","new york yankees"],"short_url":"http:\/\/tmblr.co\/ZhSWUx64503n","highlighted":[],"note_count":8,"caption":"\u003Cp\u003Elast one for tonight. it’s pretty good.\u003C\/p\u003E\n\u003Cp\u003E\u003Cspan\u003E\u003Cstrong\u003E\u003Ca href=\u0022http:\/\/www.imdb.com\/name\/nm0569000\/\u0022\u003EMr. Parker\u003C\/a\u003E\u003C\/strong\u003E: It’s a Major Award!\u00a0\u003Cbr\/\u003E\u003Cstrong\u003E\u003Ca href=\u0022http:\/\/www.imdb.com\/name\/nm0163706\/\u0022\u003ESwede\u003C\/a\u003E\u003C\/strong\u003E: Shucks, I wouldn’t know that. It looks like a lamp.\u00a0\u003Cbr\/\u003E\u003Cstrong\u003E\u003Ca href=\u0022http:\/\/www.imdb.com\/name\/nm0569000\/\u0022\u003EMr. Parker\u003C\/a\u003E\u003C\/strong\u003E: What is a lamp, you nincompoop? It’s a Major Award. I won it!\u00a0\u003Cbr\/\u003E\u003C\/span\u003E\u003C\/p\u003E\n\u003Cp\u003E\u003Cspan\u003E\u003Cstrong\u003E\u003Cbr\/\u003E\u003C\/strong\u003E\u003C\/span\u003E\u003C\/p\u003E","link_url":"http:\/\/www.huntauctions.com\/live\/imageviewer.cfm?auction_num=32&lot_num=176&lot_qual=","image_permalink":"http:\/\/pitchersandpoets.tumblr.com\/image\/6510870769","photos":[{"caption":"","alt_sizes":[{"width":450,"height":764,"url":"http:\/\/38.media.tumblr.com\/tumblr_lmrgixiNBG1qk9es5o1_1280.jpg"},{"width":442,"height":750,"url":"http:\/\/37.media.tumblr.com\/tumblr_lmrgixiNBG1qk9es5o1_500.jpg"},{"width":353,"height":600,"url":"http:\/\/37.media.tumblr.com\/tumblr_lmrgixiNBG1qk9es5o1_400.jpg"},{"width":236,"height":400,"url":"http:\/\/31.media.tumblr.com\/tumblr_lmrgixiNBG1qk9es5o1_250.jpg"},{"width":100,"height":170,"url":"http:\/\/38.media.tumblr.com\/tumblr_lmrgixiNBG1qk9es5o1_100.jpg"},{"width":75,"height":75,"url":"http:\/\/38.media.tumblr.com\/tumblr_lmrgixiNBG1qk9es5o1_75sq.jpg"}],"original_size":{"width":450,"height":764,"url":"http:\/\/38.media.tumblr.com\/tumblr_lmrgixiNBG1qk9es5o1_1280.jpg"},"exif":{"Camera":"Nikon D2x","ISO":200,"Aperture":"f\/22","Exposure":"1\/250th","FocalLength":"52mm"}}]},{"blog_name":"pitchersandpoets","id":6476502556,"post_url":"http:\/\/pitchersandpoets.tumblr.com\/post\/6476502556\/1923-yankees-on-opening-day","slug":"1923-yankees-on-opening-day","type":"photo","date":"2011-06-13 03:08:23 GMT","timestamp":1307934503,"state":"published","format":"html","reblog_key":"leaBIPKS","tags":["cardigans","new york yankees","baseball"],"short_url":"http:\/\/tmblr.co\/ZhSWUx621vOS","highlighted":[],"note_count":5,"caption":"\u003Cp\u003E1923 yankees on opening day.\u003C\/p\u003E","link_url":"http:\/\/www.baseball-fever.com","image_permalink":"http:\/\/pitchersandpoets.tumblr.com\/image\/6476502556","photos":[{"caption":"","alt_sizes":[{"width":1252,"height":845,"url":"http:\/\/38.media.tumblr.com\/tumblr_lmpkpzFvFX1qk9es5o1_1280.jpg"},{"width":500,"height":337,"url":"http:\/\/37.media.tumblr.com\/tumblr_lmpkpzFvFX1qk9es5o1_500.jpg"},{"width":400,"height":270,"url":"http:\/\/33.media.tumblr.com\/tumblr_lmpkpzFvFX1qk9es5o1_400.jpg"},{"width":250,"height":169,"url":"http:\/\/38.media.tumblr.com\/tumblr_lmpkpzFvFX1qk9es5o1_250.jpg"},{"width":100,"height":67,"url":"http:\/\/37.media.tumblr.com\/tumblr_lmpkpzFvFX1qk9es5o1_100.jpg"},{"width":75,"height":75,"url":"http:\/\/38.media.tumblr.com\/tumblr_lmpkpzFvFX1qk9es5o1_75sq.jpg"}],"original_size":{"width":1252,"height":845,"url":"http:\/\/38.media.tumblr.com\/tumblr_lmpkpzFvFX1qk9es5o1_1280.jpg"}}]},{"blog_name":"pitchersandpoets","id":6451874521,"post_url":"http:\/\/pitchersandpoets.tumblr.com\/post\/6451874521\/photo-taken-from-inside-the-polo-grounds-1936","slug":"photo-taken-from-inside-the-polo-grounds-1936","type":"photo","date":"2011-06-12 13:29:32 GMT","timestamp":1307885372,"state":"published","format":"html","reblog_key":"9qhC8fCw","tags":["new york baseball giants","new york yankees","baseball"],"short_url":"http:\/\/tmblr.co\/ZhSWUx60ZyhP","highlighted":[],"note_count":6,"caption":"\u003Cp\u003Ephoto taken from inside the Polo Grounds, 1936 World Series\u003C\/p\u003E","image_permalink":"http:\/\/pitchersandpoets.tumblr.com\/image\/6451874521","photos":[{"caption":"","alt_sizes":[{"width":1024,"height":823,"url":"http:\/\/37.media.tumblr.com\/tumblr_lmoit7M0Tu1qk9es5o1_1280.jpg"},{"width":500,"height":402,"url":"http:\/\/38.media.tumblr.com\/tumblr_lmoit7M0Tu1qk9es5o1_500.jpg"},{"width":400,"height":321,"url":"http:\/\/38.media.tumblr.com\/tumblr_lmoit7M0Tu1qk9es5o1_400.jpg"},{"width":250,"height":201,"url":"http:\/\/37.media.tumblr.com\/tumblr_lmoit7M0Tu1qk9es5o1_250.jpg"},{"width":100,"height":80,"url":"http:\/\/37.media.tumblr.com\/tumblr_lmoit7M0Tu1qk9es5o1_100.jpg"},{"width":75,"height":75,"url":"http:\/\/33.media.tumblr.com\/tumblr_lmoit7M0Tu1qk9es5o1_75sq.jpg"}],"original_size":{"width":1024,"height":823,"url":"http:\/\/37.media.tumblr.com\/tumblr_lmoit7M0Tu1qk9es5o1_1280.jpg"},"exif":{"Camera":"HP Scanjet 4800"}}]},{"blog_name":"pitchersandpoets","id":6419489196,"post_url":"http:\/\/pitchersandpoets.tumblr.com\/post\/6419489196\/mantle-doris-day-cary-grant-roger-maris-from-the","slug":"mantle-doris-day-cary-grant-roger-maris-from-the","type":"photo","date":"2011-06-11 15:00:06 GMT","timestamp":1307804406,"state":"published","format":"html","reblog_key":"oEl9M79Q","tags":["new york yankees","cary grant","baseball","roger maris","mickey mantle","doris day"],"short_url":"http:\/\/tmblr.co\/ZhSWUx5_eQ6i","highlighted":[],"note_count":15,"caption":"\u003Cp\u003Emantle\/doris day\/cary grant\/roger maris\u003C\/p\u003E\n\u003Cp\u003Efrom the unreal rare photo thread on baseball-fever\u003C\/p\u003E","link_url":"http:\/\/www.baseball-fever.com\/showthread.php?75602-Bill-s-Rare-Photo-Finds.","image_permalink":"http:\/\/pitchersandpoets.tumblr.com\/image\/6419489196","photos":[{"caption":"","alt_sizes":[{"width":1024,"height":819,"url":"http:\/\/31.media.tumblr.com\/tumblr_lmlvi3xgNp1qk9es5o1_1280.jpg"},{"width":500,"height":400,"url":"http:\/\/38.media.tumblr.com\/tumblr_lmlvi3xgNp1qk9es5o1_500.jpg"},{"width":400,"height":320,"url":"http:\/\/38.media.tumblr.com\/tumblr_lmlvi3xgNp1qk9es5o1_400.jpg"},{"width":250,"height":200,"url":"http:\/\/38.media.tumblr.com\/tumblr_lmlvi3xgNp1qk9es5o1_250.jpg"},{"width":100,"height":80,"url":"http:\/\/37.media.tumblr.com\/tumblr_lmlvi3xgNp1qk9es5o1_100.jpg"},{"width":75,"height":75,"url":"http:\/\/37.media.tumblr.com\/tumblr_lmlvi3xgNp1qk9es5o1_75sq.jpg"}],"original_size":{"width":1024,"height":819,"url":"http:\/\/31.media.tumblr.com\/tumblr_lmlvi3xgNp1qk9es5o1_1280.jpg"}}]},{"blog_name":"pitchersandpoets","id":6371679809,"post_url":"http:\/\/pitchersandpoets.tumblr.com\/post\/6371679809\/via-forall2envy-jacked-from-the-blog-of-one-of-my","slug":"via-forall2envy-jacked-from-the-blog-of-one-of-my","type":"photo","date":"2011-06-10 02:11:31 GMT","timestamp":1307671891,"state":"published","format":"html","reblog_key":"QmcQWtuW","tags":["milwaukee braves","new york yankees","chief nock-a-homa","baseball"],"short_url":"http:\/\/tmblr.co\/ZhSWUx5xo1v1","highlighted":[],"note_count":30,"caption":"\u003Cp\u003Evia forall2envy\u003C\/p\u003E\n\u003Cp\u003Ejacked from the blog of one of my secret favorite things: 1990s sports gear resale shops.\u003C\/p\u003E\n\u003Cp\u003Eis chief nock-a-homa laughing or shouting? or yawning?\u003C\/p\u003E","link_url":"http:\/\/forall2envy.blogspot.com\/2011\/06\/comin-with-more-hits-than-braves-and_09.html","image_permalink":"http:\/\/pitchersandpoets.tumblr.com\/image\/6371679809","photos":[{"caption":"","alt_sizes":[{"width":723,"height":934,"url":"http:\/\/31.media.tumblr.com\/tumblr_lmjy367V5z1qk9es5o1_1280.jpg"},{"width":500,"height":646,"url":"http:\/\/33.media.tumblr.com\/tumblr_lmjy367V5z1qk9es5o1_500.jpg"},{"width":400,"height":517,"url":"http:\/\/31.media.tumblr.com\/tumblr_lmjy367V5z1qk9es5o1_400.jpg"},{"width":250,"height":323,"url":"http:\/\/37.media.tumblr.com\/tumblr_lmjy367V5z1qk9es5o1_250.jpg"},{"width":100,"height":129,"url":"http:\/\/37.media.tumblr.com\/tumblr_lmjy367V5z1qk9es5o1_100.jpg"},{"width":75,"height":75,"url":"http:\/\/38.media.tumblr.com\/tumblr_lmjy367V5z1qk9es5o1_75sq.jpg"}],"original_size":{"width":723,"height":934,"url":"http:\/\/31.media.tumblr.com\/tumblr_lmjy367V5z1qk9es5o1_1280.jpg"}}]}],"total_posts":10}}'

# I try to load the json formatted string
blog_posts = json.loads(json_from_doc)

输出

  Traceback (most recent call last):
 File "G:\programming\python\json_test.py", line 23, in <module>
   blog_posts = json.loads(json_from_doc)
 File "C:\Python27\lib\json\__init__.py", line 338, in loads
   return _default_decoder.decode(s)
 File "C:\Python27\lib\json\decoder.py", line 366, in decode
   obj, end = self.raw_decode(s, idx=_w(s, 0).end())
 File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
   obj, end = self.scan_once(s, idx)
 ValueError: Invalid control character at: line 1 column 1307 (char 1306) 

如何从字符串中加载文档?

是否有一个选项告诉Python不关心invalid control character,因为它在文件中时并不关心它?

2 个答案:

答案 0 :(得分:3)

看来我很笨。

使用原始字符串文字解决了该问题:

json_from_doc = r''' <json> '''

答案 1 :(得分:1)

您需要在此类复制和粘贴操作中转义反斜杠,使用原始字符串:

json_from_doc = r'<your json content here>'

否则python会将所有反斜杠解释为特殊字符,在将数据传递给json之前弄乱数据。