首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >大熊猫阅读中json文件的url不起作用

大熊猫阅读中json文件的url不起作用
EN

Stack Overflow用户
提问于 2020-07-02 15:38:48
回答 2查看 395关注 0票数 1

我想阅读json文件使用url在熊猫,但它给我一些错误,我认为这是一个相关的路径,我已经给出,请看代码和网址。我没有在堆栈溢出中找到它,所以问它。这个可能是复制的但请帮帮我伙计们。

代码语言:javascript
复制
data_df = pd.read_json('https://github.com/jackiekazil/data-wrangling/blob/master/data/chp3/data-text.json')

json_url如下所示:

https://github.com/jackiekazil/data-wrangling/blob/master/data/chp3/data-text.json

错误信息:

代码语言:javascript
复制
ValueError: Expected object or value
EN

回答 2

Stack Overflow用户

发布于 2020-07-02 19:37:36

您需要这个网址:https://raw.githubusercontent.com/jackiekazil/data-wrangling/master/data/chp3/data-text.json

raw.githubusercontent.com提供在Github存储库中找到的未处理文件。您在问题中发布的链接不会为您提供您感兴趣的原始JSON文件;相反,它会加载网页本身。它也是在this answer.中解决的

使用上面的URL,您的代码对我来说很好。

票数 1
EN

Stack Overflow用户

发布于 2020-07-03 02:09:07

代码语言:javascript
复制
- [`requests`](https://requests.readthedocs.io/en/master/)
  • 顺便说一句,下面的函数将用于从正确的URL下载任何文件。
    • 函数需要保存到的URL和目录。

代码语言:javascript
复制
import request
from pathlib import Path
import pandas as pd

def create_dir_save_file(dir_path: Path, url: str):
    """
    Check if the path exists and create it if it does not.
    Check if the file exists and download it if it does not.
    """
    if not dir_path.parents[0].exists():  # if directory doesn't exist
        dir_path.parents[0].mkdir(parents=True)  # create directory
        print(f'Directory Created: {dir_path.parents[0]}')
    else:
        print('Directory Exists')
        
    if not dir_path.exists():  # if file doesn't exist
        r = requests.get(url, allow_redirects=True)  # get file
        open(dir_path, 'wb').write(r.content)  # write file
        print(f'File Created: {dir_path.name}')
    else:
        print('File Exists')
        

data_dir = Path.cwd()  # current working dir or use Path('e:/some_path') to specify a location
url = 'https://raw.githubusercontent.com/jackiekazil/data-wrangling/master/data/chp3/data-text.json'
file_name = url.split('/')[-1]
data_path = data_dir / file_name  # local path to data once downloaded
create_dir_save_file(data_path, url)  # call function to download file

df = pd.json_normalize(data_path)  create dataframe

# display(df.head())

                           Indicator PUBLISH STATES  Year             WHO region World Bank income group               Country         Sex  Display Value  Numeric Low High Comments
0   Life expectancy at birth (years)      Published  1990                 Europe             High-income               Andorra  Both sexes             77     77.0                  
1   Life expectancy at birth (years)      Published  2000                 Europe             High-income               Andorra  Both sexes             80     80.0                  
2  Life expectancy at age 60 (years)      Published  2012                 Europe             High-income               Andorra      Female             28     28.0                  
3  Life expectancy at age 60 (years)      Published  2000                 Europe             High-income               Andorra  Both sexes             23     23.0                  
4   Life expectancy at birth (years)      Published  2012  Eastern Mediterranean             High-income  United Arab Emirates      Female             78     78.0                  
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62700072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档