2016-09-28 17 views
1

그래서 난 JSON 작업에 새로운 해요 오브젝트와 나는 openrecipe database from here. 당신이 얻는 DB 덤프를 해결하려고 노력하고있어 읽는 방법과 같은 모습 ...파이썬 3.5 : JSON의 DB를

{ "_id" : { "$oid" : "5160756d96cc62079cc2db16" }, "name" : "Hot Roast Beef Sandwiches", "ingredients" : "12 whole Dinner Rolls Or Small Sandwich Buns (I Used Whole Wheat)\n1 pound Thinly Shaved Roast Beef Or Ham (or Both!)\n1 pound Cheese (Provolone, Swiss, Mozzarella, Even Cheez Whiz!)\n1/4 cup Mayonnaise\n3 Tablespoons Grated Onion (or 1 Tbsp Dried Onion Flakes))\n1 Tablespoon Poppy Seeds\n1 Tablespoon Spicy Mustard\n1 Tablespoon Horseradish Mayo Or Straight Prepared Horseradish\n Dash Of Worcestershire\n Optional Dressing Ingredients: Sriracha, Hot Sauce, Dried Onion Flakes Instead Of Fresh, Garlic Powder, Pepper, Etc.)", "url" : "http://thepioneerwoman.com/cooking/2013/03/hot-roast-beef-sandwiches/", "image" : "http://static.thepioneerwoman.com/cooking/files/2013/03/sandwiches.jpg", "ts" : { "$date" : 1365276013902 }, "cookTime" : "PT20M", "source" : "thepioneerwoman", "recipeYield" : "12", "datePublished" : "2013-03-13", "prepTime" : "PT20M", "description" : "When I was growing up, I participated in my Episcopal church's youth group, and I have lots of memories of weekly meetings wh..." } 
{ "_id" : { "$oid" : "5160756f96cc6207a37ff777" }, "name" : "Morrocan Carrot and Chickpea Salad", "ingredients" : "Dressing:\n1 tablespoon cumin seeds\n1/3 cup/80 ml extra virgin olive oil\n2 tablespoons fresh lemon juice\n1 tablespoon honey\n1/2 teaspoon fine sea salt, plus more to taste\n1/8 teaspoon cayenne pepper\n10 ounces carrots, shredded on a box grater or sliced whisper thin on a mandolin\n2 cups cooked chickpeas (or one 15- ounce can, drained and rinsed)\n2/3 cup/100 g dried pluots, plums, or dates cut into chickpea-sized pieces\n1/3 cup/30 g fresh mint, torn\nFor serving: lots of toasted almond slices, dried or fresh rose petals - all optional (but great additions!)", "url" : "http://www.101cookbooks.com/archives/moroccan-carrot-and-chickpea-salad-recipe.html", "image" : "http://www.101cookbooks.com/mt-static/images/food/moroccan_carrot_salad_recipe.jpg", "ts" : { "$date" : 1365276015332 }, "datePublished" : "2013-01-07", "source" : "101cookbooks", "prepTime" : "PT15M", "description" : "A beauty of a carrot salad - tricked out with chickpeas, chunks of dried pluots, sliced almonds, and a toasted cumin dressing. Thank you Diane Morgan." } 
{ "_id" : { "$oid" : "5160757096cc62079cc2db17" }, "name" : "Mixed Berry Shortcake", "ingredients" : "Biscuits\n3 cups All-purpose Flour\n2 Tablespoons Baking Powder\n3 Tablespoons Sugar\n1/2 teaspoon Salt\n1-1/2 stick (3/4 Cup) Cold Butter, Cut Into Pieces\n1-1/4 cup Buttermilk\n1/2 teaspoon Almond Extract (optional)\n Berries\n2 pints Mixed Berries And/or Sliced Strawberries\n1/3 cup Sugar\n Zest And Juice Of 1 Small Orange\n SWEET YOGURT CREAM\n1 package (7 Ounces) Plain Greek Yogurt\n1 cup Cold Heavy Cream\n1/2 cup Sugar\n2 Tablespoons Brown Sugar", "url" : "http://thepioneerwoman.com/cooking/2013/03/mixed-berry-shortcake/", "image" : "http://static.thepioneerwoman.com/cooking/files/2013/03/shortcake.jpg", "ts" : { "$date" : 1365276016700 }, "cookTime" : "PT15M", "source" : "thepioneerwoman", "recipeYield" : "8", "datePublished" : "2013-03-18", "prepTime" : "PT15M", "description" : "It's Monday! It's a brand new week! The birds are chirping! The coffee's brewing! Everything has such hope and promise!  A..." } 

은 내가 다음 역 추적

Traceback (most recent call last): 
    File "C:/Users/<redacted>/Documents/<redacted>/project/test_json.py", line 7, in <module> 
    data = json.load(dfile) 
    File "C:\Users\<redacted>\AppData\Local\Continuum\Anaconda3\Lib\json\__init__.py", line 265, in load 
    return loads(fp.read(), 
    File "C:\Users\<redacted>\AppData\Local\Continuum\Anaconda3\Lib\encodings\cp1252.py", line 23, in decode 
    return codecs.charmap_decode(input,self.errors,decoding_table)[0] 
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 101915: character maps to <undefined> 

오을받은 데이터베이스에두고

import json 

f = r'<file_path>\recipeitems-latest.json' 

with open(f) as dfile: 
    data = json.load(dfile) 

print(data) 

을 읽을 수있는 다음 코드를 시도 이 오류를 해결할 수있는 nly 방법은 json 파일에 하나의 항목 만있는 것입니다. db 형식이 잘못되었거나 데이터를 잘못 읽었습니까?

도움 주셔서 감사합니다.

+0

전체적으로 파일을 구문 분석하면 오류가 발생합니다. 그것은 유효한 json이 아니다. 그것은 개별적인 유효한 json 라인의 낱단이다. 그러나 당신은 그들을 모두 한 번에 파싱 할 수 없다. 만약 당신이 한줄을 읽고, 작동해야하는 줄을 파싱했다면. –

+0

알았습니다. 감사합니다 마크 – njfrazie

답변

1

파일이 json 배열이 아닙니다. 파일의 각 줄은 json 문서이지만 전체 파일은 json 형식이 아닙니다.

하는 선으로 파일을 읽고, json.loads를 사용

with open('some_file') as f: 
    for line in f: 
    doc = json.loads(line) 

당신은 또한 open()encoding 매개 변수를 전달해야 할 수도 있습니다. See here.

+0

감사 slezica! – njfrazie