Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解析json文件_Json_Python 3.x_Pandas_Twitter - Fatal编程技术网

解析json文件

解析json文件,json,python-3.x,pandas,twitter,Json,Python 3.x,Pandas,Twitter,下面是在.json文件中获取的一组tweet的可视化分析代码。解释时,map()函数会显示一个错误。有办法解决吗 import json import pandas as pd import matplotlib.pyplot as plt tweets_data_path = 'import_requests.txt' tweets_data = [] tweets_file = open(tweets_data_path, "r") for line in tweets_file:

下面是在.json文件中获取的一组tweet的可视化分析代码。解释时,map()函数会显示一个错误。有办法解决吗

import json
import pandas as pd
import matplotlib.pyplot as plt


tweets_data_path = 'import_requests.txt'

tweets_data = []
tweets_file = open(tweets_data_path, "r")

for line in tweets_file:
   try:
    tweet = json.loads(line)
    tweets_data.append(tweet)
   except:
      continue

print(len(tweets_data))

tweets = pd.DataFrame()

tweets['text'] = map(lambda tweet: tweet['text'], tweets_data)
以下是我收到的关于上述代码的“ValueError”消息的前导行:

回溯(最近一次呼叫最后一次): 文件“tweet_len.py”,第21行,在 tweets['text']=map(lambda tweet:tweet['text'],tweets\u数据)
文件“/usr/lib/python3/dist packages/pandas/core/frame.py”,第1887行,位于setitem 自我设置项目(键、值)
文件“/usr/lib/python3/dist packages/pandas/core/frame.py”,第1966行,在集合项中
self.\u确保\u有效\u索引(值) 文件“/usr/lib/python3/dist packages/pandas/core/frame.py”,第1943行,在索引中确保有效
raise VALUERROR('无法设置没有定义索引的帧' ValueError:无法设置没有定义索引的帧和无法转换为序列的值

我在用蟒蛇3

编辑:下面是收集的twitter数据样本(.json格式)

我认为你可以使用:

在实体上创建的贡献者坐标\ 撰稿人:楠楠2016-03-05 05:47:23楠楠 创建于NaN NaN 2016-03-05 05:47:23 NaN 默认配置文件NaN NaN 2016-03-05 05:47:23 NaN 默认配置文件图像NaN NaN 2016-03-05 05:47:23 NaN 描述楠楠2016-03-05 05:47:23楠楠 收藏夹\u计数收藏夹过滤器\u级别地理位置\ 贡献者\u已启用0错误低NaN 在0处创建了_假低NaN 默认_配置文件0假低NaN 默认\u配置文件\u图像0假低NaN 说明0假低NaN id_str\ 贡献者_已启用705993088574033920 705993088574033920 已在705993088574033920 705993088574033920处创建 默认配置文件705993088574033920 705993088574033920 默认_配置文件_图像705993088574033920 705993088574033920 说明705993088574033920 705993088574033920 …是状态语言吗\ 已启用贡献者\u…错误en 在…上创建了\u假en 默认配置文件…错误 默认\u配置文件\u图像…False en 描述…虚假 位置转发\u已转发的计数\ 参与者\u已启用NaN 0错误 在NaN 0处创建了_False 默认配置文件NaN 0错误 默认\u配置文件\u图像NaN 0错误 说明0错误 来源\
您可以添加
json
文件的示例吗?谢谢。但是此
json
无效。请检查。在运行上述代码段的过程中,我收到了一条“ValueError”错误消息。有趣的是,我仍然收到相同的错误-ValueError:尾部数据错误消息。嗯,原始
json
是否有效?如果是,我s是否可以共享此
文件
?这是指向json文件的链接-它与Python版本有关吗?
{
    "created_at": "Sat Mar 05 05:47:23 +0000 2016",
    "id": 705993088574033920,
    "id_str": "705993088574033920",
    "text": "Tumi Inc. civil war: Staff manning US ceasefire hotline 'can't speak Arabic' #fakeheadlinebot #learntocode #makeatwitterbot #javascript",
    "source": "\u003ca href=\"http://javascriptiseasy.com\" rel=\"nofollow\"\u003eJavaScript is Easy\u003c/a\u003e",
    "truncated": false,
    "in_reply_to_status_id": null,
    "in_reply_to_status_id_str": null,
    "in_reply_to_user_id": null,
    "in_reply_to_user_id_str": null,
    "in_reply_to_screen_name": null,
    "user": {
        "id": 4382400263,
        "id_str": "4382400263",
        "name": "JavaScript is Easy",
        "screen_name": "javascriptisez",
        "location": "Your Console",
        "url": "http://javascriptiseasy.com",
        "description": "Get learning!",
        "protected": false,
        "verified": false,
        "followers_count": 167,
        "friends_count": 68,
        "listed_count": 212,
        "favourites_count": 11,
        "statuses_count": 55501,
        "created_at": "Sat Dec 05 11:18:00 +0000 2015",
        "utc_offset": null,
        "time_zone": null,
        "geo_enabled": false,
        "lang": "en",
        "contributors_enabled": false,
        "is_translator": false,
        "profile_background_color": "000000",
        "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png",
        "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png",
        "profile_background_tile": false,
        "profile_link_color": "FFCC4D",
        "profile_sidebar_border_color": "000000",
        "profile_sidebar_fill_color": "000000",
        "profile_text_color": "000000",
        "profile_use_background_image": false,
        "profile_image_url": "http://pbs.twimg.com/profile_images/673099606348070912/xNxp4zOt_normal.jpg",
        "profile_image_url_https": "https://pbs.twimg.com/profile_images/673099606348070912/xNxp4zOt_normal.jpg",
        "profile_banner_url": "https://pbs.twimg.com/profile_banners/4382400263/1449314370",
        "default_profile": false,
        "default_profile_image": false,
        "following": null,
        "follow_request_sent": null,
        "notifications": null
    },
    "geo": null,
    "coordinates": null,
    "place": null,
    "contributors": null,
    "is_quote_status": false,
    "retweet_count": 0,
    "favorite_count": 0,
    "entities": {
        "hashtags": [{
            "text": "fakeheadlinebot",
            "indices": [77, 93]
        }, {
            "text": "learntocode",
            "indices": [94, 106]
        }, {
            "text": "makeatwitterbot",
            "indices": [107, 123]
        }, {
            "text": "javascript",
            "indices": [124, 135]
        }],
        "urls": [],
        "user_mentions": [],
        "symbols": []
    },
    "favorited": false,
    "retweeted": false,
    "filter_level": "low",
    "lang": "en",
    "timestamp_ms": "1457156843690"
}
import pandas as pd

df = pd.read_json('file.json')
print df.head()
                       contributors  coordinates          created_at entities  \
contributors_enabled            NaN          NaN 2016-03-05 05:47:23      NaN   
created_at                      NaN          NaN 2016-03-05 05:47:23      NaN   
default_profile                 NaN          NaN 2016-03-05 05:47:23      NaN   
default_profile_image           NaN          NaN 2016-03-05 05:47:23      NaN   
description                     NaN          NaN 2016-03-05 05:47:23      NaN   

                       favorite_count favorited filter_level  geo  \
contributors_enabled                0     False          low  NaN   
created_at                          0     False          low  NaN   
default_profile                     0     False          low  NaN   
default_profile_image               0     False          low  NaN   
description                         0     False          low  NaN   

                                       id              id_str  \
contributors_enabled   705993088574033920  705993088574033920   
created_at             705993088574033920  705993088574033920   
default_profile        705993088574033920  705993088574033920   
default_profile_image  705993088574033920  705993088574033920   
description            705993088574033920  705993088574033920   

                                    ...                is_quote_status  lang  \
contributors_enabled                ...                          False    en   
created_at                          ...                          False    en   
default_profile                     ...                          False    en   
default_profile_image               ...                          False    en   
description                         ...                          False    en   

                       place  retweet_count  retweeted  \
contributors_enabled     NaN              0      False   
created_at               NaN              0      False   
default_profile          NaN              0      False   
default_profile_image    NaN              0      False   
description              NaN              0      False   

                                                                  source  \
contributors_enabled   <a href="http://javascriptiseasy.com" rel="nof...   
created_at             <a href="http://javascriptiseasy.com" rel="nof...   
default_profile        <a href="http://javascriptiseasy.com" rel="nof...   
default_profile_image  <a href="http://javascriptiseasy.com" rel="nof...   
description            <a href="http://javascriptiseasy.com" rel="nof...   

                                                                    text  \
contributors_enabled   Tumi Inc. civil war: Staff manning US ceasefir...   
created_at             Tumi Inc. civil war: Staff manning US ceasefir...   
default_profile        Tumi Inc. civil war: Staff manning US ceasefir...   
default_profile_image  Tumi Inc. civil war: Staff manning US ceasefir...   
description            Tumi Inc. civil war: Staff manning US ceasefir...   

                                 timestamp_ms  truncated  \
contributors_enabled  2016-03-05 05:47:23.690      False   
created_at            2016-03-05 05:47:23.690      False   
default_profile       2016-03-05 05:47:23.690      False   
default_profile_image 2016-03-05 05:47:23.690      False   
description           2016-03-05 05:47:23.690      False   

                                                 user  
contributors_enabled                            False  
created_at             Sat Dec 05 11:18:00 +0000 2015  
default_profile                                 False  
default_profile_image                           False  
description                             Get learning!  

[5 rows x 25 columns]