【发布时间】:2021-10-16 17:02:55
【问题描述】:
我有一个 json 文件,我在 jupyter 实验室的笔记本中工作时尝试将其加载到 pandas DataFrame 中。我为此使用以下代码:
import pandas as pd
df = pd.read_json('goodreads_books_poetry.json', lines=True)
但是我得到的错误是“ValueError: Expected object or value”。
奇怪的是,如果我把它塞进一个 python 文件并在终端上运行它,它工作得很好,如果它不是那么慢和不方便探索数据,我会使用这个方法。
这是数据的简短sn-p
{"isbn": "", "text_reviews_count": "1", "series": [], "country_code": "US", "language_code": "eng", "popular_shelves": [{"count": "8", "name": "to-read"}, {"count": "3", "name": "poetry"}, {"count": "2", "name": "currently-reading"}, {"count": "1", "name": "01-kindle"}, {"count": "1", "name": "real-books"}, {"count": "1", "name": "personal-library"}], "asin": "", "is_ebook": "false", "average_rating": "3.83", "kindle_asin": "", "similar_books": [], "description": "Number 30 in a series of literary pamphlets published monthly and available at the price of 15 cents per copy, or a yearly subscription (19 numbers) for $1.25", "format": "Paperback", "link": "https://www.goodreads.com/book/show/16037549-vision-of-sir-launfal-and-other-poems", "authors": [{"author_id": "15585", "role": ""}], "publisher": "Houghton, Mifflin and Company", "num_pages": "80", "publication_day": "1", "isbn13": "", "publication_month": "11", "edition_information": "", "publication_year": "1887", "url": "https://www.goodreads.com/book/show/16037549-vision-of-sir-launfal-and-other-poems", "image_url": "https://images.gr-assets.com/books/1348176637m/16037549.jpg", "book_id": "16037549", "ratings_count": "3", "work_id": "5212748", "title": "Vision of Sir Launfal and Other Poems", "title_without_series": "Vision of Sir Launfal and Other Poems"}
{"isbn": "0811223981", "text_reviews_count": "2", "series": [], "country_code": "US", "language_code": "", "popular_shelves": [{"count": "100", "name": "to-read"}, {"count": "6", "name": "currently-reading"}, {"count": "3", "name": "drama"}, {"count": "3", "name": "plays"}, {"count": "2", "name": "books-i-own"}, {"count": "2", "name": "play"}, {"count": "2", "name": "read-2015"}, {"count": "1", "name": "golden"}, {"count": "1", "name": "poetry-and-plays"}, {"count": "1", "name": "retelling"}, {"count": "1", "name": "myth-fairy-tale"}, {"count": "1", "name": "fairytaleish"}, {"count": "1", "name": "favorites-2015"}, {"count": "1", "name": "2015-2016"}, {"count": "1", "name": "undated"}, {"count": "1", "name": "translated-fiction"}, {"count": "1", "name": "short-story-tbr"}, {"count": "1", "name": "jen-campbell-recs"}, {"count": "1", "name": "poetry"}, {"count": "1", "name": "theater"}, {"count": "1", "name": "to-buy"}, {"count": "1", "name": "maybe"}, {"count": "1", "name": "deutsch"}, {"count": "1", "name": "1910s"}, {"count": "1", "name": "to-read2"}, {"count": "1", "name": "new-directions"}, {"count": "1", "name": "tbp-dno"}, {"count": "1", "name": "fairytale-retellings"}, {"count": "1", "name": "published-2015"}, {"count": "1", "name": "translation"}, {"count": "1", "name": "fiction"}, {"count": "1", "name": "19th-early-20th"}, {"count": "1", "name": "german-language"}, {"count": "1", "name": "not-at-library"}, {"count": "1", "name": "flavorwire"}], "asin": "", "is_ebook": "false", "average_rating": "3.83", "kindle_asin": "B00U2WY9U8", "similar_books": [], "description": "Fairy Tales gathers the unconventional verse dramolettes of the Swiss writer Robert Walser. Narrated in Walser's inimitable, playful language, these theatrical pieces overturn traditional notions of the fairy tale, transforming the Brothers Grimm into metatheater, even metareflections.\nSnow White forgives the evil queen for trying to kill her, Cinderella doubts her prince and enjoys being hated by her evil stepsisters; the Fairy Tale itself is a character who encourages her to stay within the confines of the story. Sleeping Beauty, the royal family, and its retainers are not happy about being woken from their sleep by an absurd, unpretentious, Walser-like hero. Mary and Joseph are taken aback by what lies in store for their baby Jesus.", "format": "Paperback", "link": "https://www.goodreads.com/book/show/22466716-fairy-tales", "authors": [{"author_id": "16073", "role": ""}, {"author_id": "3484503", "role": "Translator"}, {"author_id": "367928", "role": "Translator"}, {"author_id": "1028358", "role": "Preface"}], "publisher": "New Directions", "num_pages": "128", "publication_day": "20", "isbn13": "9780811223980", "publication_month": "4", "edition_information": "", "publication_year": "2015", "url": "https://www.goodreads.com/book/show/22466716-fairy-tales", "image_url": "https://images.gr-assets.com/books/1404958407m/22466716.jpg", "book_id": "22466716", "ratings_count": "37", "work_id": "41905435", "title": "Fairy Tales: Dramolettes", "title_without_series": "Fairy Tales: Dramolettes"}
我实际上使用的是来自这里的相同代码:https://towardsdatascience.com/recommendation-system-in-python-lightfm-61c85010ce17,因为我正在尝试使用他们的文章和数据集来探索 lightfm 模型,所以我不明白出了什么问题。 我也玩了一点 orient 参数,但没有任何成功。
【问题讨论】:
-
你可以试试
pd.json_normalize('goodreads_books_poetry.json')吗?这行得通吗? -
你的python和json文件在同一个文件夹吗?
-
@Umar.H 感谢您的建议,不幸的是,这给出了“NotImplementedError”
-
@R.Marolahy 是的,他们是
-
@Chloe 你能更新你的熊猫版本吗?我无法使用上述方法很好地读取您的 json 文件
标签: python json pandas jupyter-notebook jupyter-lab