2014-04-28 29 views
1

首先,如果我的描述不够准确,我很抱歉,我是一个新手,我不知道有关编程的事情,所以不要犹豫告诉我,如果你需要更详细的信息,但我会尽量做到尽可能精确。JSON到CSV:如何在最终的Excel表格中添加过滤器(列)?

所以我已经下载了一堆感谢的鸣叫到Twitter的API和终端(通过Twurl)。所有的推文都在.json文件中(我用TextWrangler打开,我在Mac上),事情是当我将.json文件导出到.csv文件以便更容易地处理和分析数据时感谢Excel(或者至少是LibreOffice的Excel版本),我没有我需要的所有参数用于学习,我缺少.json文件中每个Tweet信息的“生物”部分。换句话说,在我的决赛桌上,我有一个推文ID列,一个推文作者,一个推文本身,等等......但我没有列的生物tweet作者,而这些信息显示在.json文件中。所以我的问题是:是否有代码或任何可以让我有更多的列显示我的最终.csv表中的基本.json文件中存在更多信息?

同样,这可能不是很清楚,所以不要犹豫,如果你需要我强调一个特定的点告诉我。

在此先感谢您的任何见解,我真的需要这方面的帮助,这是一个我需要继续为我的博士学习的研究项目,所以任何帮助都将非常值得欢迎!

编辑:作为一个例子,这里是我在原来以.json文件有一个推特数据的样本:

{ 
    "created_at": "Mon Apr 28 09:00:40 +0000 2014", 
    "id": 460705144846712800, 
    "id_str": "460705144846712832", 
    "text": "Work can suck a dick today", 
    "source": "<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone</a>", 
    "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": 253350311, 
     "id_str": "253350311", 
     "name": "JEEEZUS", 
     "screen_name": "Maxi_Flex", 
     "location": "Southchestershire", 
     "url": "http://www.soundcloud.com/maxi_flex", 
     "description": "Jazz Personality.G Mentality.", 
     "protected": false, 
     "followers_count": 457, 
     "friends_count": 400, 
     "listed_count": 1, 
     "created_at": "Thu Feb 17 02:08:57 +0000 2011", 
     "favourites_count": 1229, 
     "utc_offset": null, 
     "time_zone": null, 
     "geo_enabled": true, 
     "verified": false, 
     "statuses_count": 13661, 
     "lang": "en", 
     "contributors_enabled": false, 
     "is_translator": false, 
     "is_translation_enabled": false, 
     "profile_background_color": "08ABFC", 
     "profile_background_image_url": "http://pbs.twimg.com/profile_background_images/444297891977244672/Z1BkfCFB.jpeg", 
     "profile_background_image_url_https": "https://pbs.twimg.com/profile_background_images/444297891977244672/Z1BkfCFB.jpeg", 
     "profile_background_tile": true, 
     "profile_image_url": "http://pbs.twimg.com/profile_images/454073282778902529/gCGicDBH_normal.jpeg", 
     "profile_image_url_https": "https://pbs.twimg.com/profile_images/454073282778902529/gCGicDBH_normal.jpeg", 
      "profile_banner_url": "https://pbs.twimg.com/profile_banners/253350311/1392339276", 
     "profile_link_color": "FA05F2", 
     "profile_sidebar_border_color": "FFFFFF", 
     "profile_sidebar_fill_color": "DDEEF6", 
     "profile_text_color": "333333", 
     "profile_use_background_image": true, 
     "default_profile": false, 
     "default_profile_image": false, 
     "following": null, 
     "follow_request_sent": null, 
     "notifications": null 
    }, 
    "geo": null, 
    "coordinates": null, 
    "place": null, 
    "contributors": null, 
    "retweet_count": 0, 
    "favorite_count": 0, 
    "entities": { 
     "hashtags": [], 
     "symbols": [], 
     "urls": [], 
     "user_mentions": [] 
    }, 
    "favorited": false, 
    "retweeted": false, 
    "filter_level": "medium", 
    "lang": "en" 
} 

所以在最后的csv文件,我有一些信息我上面提到,但我需要在csv文件中添加每个字符串的“描述”部分(粗体)。任何帮助,将不胜感激!

+1

您可以发布一些示例JSON –

+0

感谢您的支持,所以下面是一个字符串示例,表示我的json文件中包含一条推文的数据: – MitchBuchanon

+0

好吧,我没有足够的空间在评论中发布样本的示例,所以我将编辑我的原始帖子... – MitchBuchanon

回答

1

任何好的JSON到CSV转换器将工作,尝试this one。如果JSON中有些时髦,我们需要一个输入JSON的例子,以及吐出的东西。

如果你只需要一个场在命令行中输入以下命令:

cat test.json | sed -n 's/.*description\":\"\([^"]*\)\".*/Description, \1/p' > result.csv 

哪里test.json是与它的所有JSON项文件。

下面是一个例子,我跑了输出:

cat test.json | sed -n 's/.*description\":\"\([^"]*\)\".*/\1/p' 
Jazz Personality.G Mentality. 
Jazz Personality.G Mentality. 
Jazz Personality.G Mentality. 
Jazz Personality.G Mentality. 

如果该文件是非常大的,你可能需要拆分零件:

split -l N test.json part 

其中N是行数每部分。

+0

感谢您的链接,我不知道它,但事情是,它不会工作,显然是因为该文件太大...关键是我的文件非常大(大约有五万条推文),因此我需要通过Excel自动处理它......但是非常感谢您的建议,我非常感谢! – MitchBuchanon

1

问题可能是JSON是分层的,而CSV不是。我猜你只是得到顶级的JSON元素而不是嵌套的对象。例如,如果您的JSON是:

{ 
'name': 'test', 
'author': { 
    'id': 123, 
    'created': '' 
    } 
} 

您只获取'name'而不是'author.id'?如果是这种情况,请查看其他相关的问题,例如为CSV压缩JSON。flattening json to csv format

+0

感谢您的回答!是的,我得到了作者ID。我在最终的csv文件中的参数的完整列表是:“tweet id”,“tweet time”,“tweet author”,“tweet author id”“tweet language”“tweet geo”和“tweet text”。我需要添加诸如“作者描述”之类的东西来获取我需要的所有数据。 – MitchBuchanon

+0

我只是看了一下你提供给我的链接,顺便说一下,但是正如我所说,我是一个新手,所以我不太了解这个主题中发生了什么。 。我不知道大部分技术术语,所以我有点困惑......:s – MitchBuchanon

相关问题