我正嘗試將json文件轉換爲csv文件。 json文件來自tweepy。無法用python編寫csv文件
import json
import csv
fo = open('Sclass.json', 'r')
fw = open('Hasil_Tweets.csv', 'a')
for line in fo:
try:
tweet = json.loads(line)
fw.write(tweet['id'],tweet['timestamp_ms'],tweet['user']['name'],tweet['user']['statuses_count'],tweet['user']['friends_count'],tweet['user']['followers_count'],tweet['place']['bounding_box']['coordinates'],tweet['text']+"\n")
except:
continue
但是,當我打印它的作品。 當我只寫fw.write(tweet['text'])
它的作品。
感謝
雅呵,我的小白上既沒有蟒蛇和tweepy。但我的直覺說,這個問題與它自己的json文件有關。對不起我的英語不好。 這是JSON文件,它的自我:
{
"created_at": "Wed Oct 11 08:36:21 +0000 2017",
"id": 918032510927355904,
"id_str": "918032510927355904",
"text": "@irfanzayo @puisisi @tasyak Lo tuh kebiasaan overthinking \ud83d\ude24",
"display_text_range": [
28,
59
],
"source": "<a href=\"http://twitter.com/download/android\" rel=\"nofollow\">Twitter for Android</a>",
"truncated": false,
"in_reply_to_status_id": 918032029094047746,
"in_reply_to_status_id_str": "918032029094047746",
"in_reply_to_user_id": 60049976,
"in_reply_to_user_id_str": "60049976",
"in_reply_to_screen_name": "irfanzayo",
"user": {
"id": 59980455,
"id_str": "59980455",
"name": "Mutiara Sisyanni D",
"screen_name": "MutiaraSisyanni",
"location": "Jakarta, Indonesia",
"url": "http://mutiarasyn.wixsite.com/mutiarasisyanni",
"description": null,
"translator_type": "none",
"protected": false,
"verified": false,
"followers_count": 354,
"friends_count": 237,
"listed_count": 1,
"favourites_count": 326,
"statuses_count": 6507,
"created_at": "Sat Jul 25 04:31:47 +0000 2009",
"utc_offset": 25200,
"time_zone": "Jakarta",
"geo_enabled": true,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"profile_background_color": "FA8C9E",
"profile_background_image_url": "http://abs.twimg.com/images/themes/theme5/bg.gif",
"profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme5/bg.gif",
"profile_background_tile": false,
"profile_link_color": "FF8A94",
"profile_sidebar_border_color": "FFFFFF",
"profile_sidebar_fill_color": "99CC33",
"profile_text_color": "3E4415",
"profile_use_background_image": false,
"profile_image_url": "http://pbs.twimg.com/profile_images/486497248293826560/FANdzhL9_normal.jpeg",
"profile_image_url_https": "https://pbs.twimg.com/profile_images/486497248293826560/FANdzhL9_normal.jpeg",
"profile_banner_url": "https://pbs.twimg.com/profile_banners/59980455/1404826066",
"default_profile": false,
"default_profile_image": false,
"following": null,
"follow_request_sent": null,
"notifications": null
},
"geo": null,
"coordinates": null,
"place": {
"id": "66555622726ab358",
"url": "https://api.twitter.com/1.1/geo/id/66555622726ab358.json",
"place_type": "city",
"name": "Setia Budi",
"full_name": "Setia Budi, Indonesia",
"country_code": "ID",
"country": "Indonesia",
"bounding_box": {
"type": "Polygon",
"coordinates": [
[
[
106.817351,
-6.24152
],
[
106.817351,
-6.201177
],
[
106.852353,
-6.201177
],
[
106.852353,
-6.24152
]
]
]
},
"attributes": {}
},
"contributors": null,
"is_quote_status": false,
"quote_count": 0,
"reply_count": 0,
"retweet_count": 0,
"favorite_count": 0,
"entities": {
"hashtags": [],
"urls": [],
"user_mentions": [
{
"screen_name": "irfanzayo",
"name": "irfan zayanto",
"id": 60049976,
"id_str": "60049976",
"indices": [
0,
10
]
},
{
"screen_name": "puisisi",
"name": "Puisi Pancara",
"id": 32809069,
"id_str": "32809069",
"indices": [
11,
19
]
},
{
"screen_name": "tasyak",
"name": "Tasya Kurnia",
"id": 41986880,
"id_str": "41986880",
"indices": [
20,
27
]
}
],
"symbols": []
},
"favorited": false,
"retweeted": false,
"filter_level": "low",
"lang": "in",
"timestamp_ms": "1507710981481"
}
另一個錯誤
Traceback (most recent call last): File "C:\Users\User\Desktop\fase 1-20170930T062552Z-001\transformCSV.py", line 7, in tweet = json.loads(line) File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\json__init__.py", line 354, in loads return _default_decoder.decode(s) File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 339, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 357, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 1)
Traceback (most recent call last):
File "C:\Users\Tanabata\Desktop\Putang ina mo\spli.py", line 8, in <module>
tweet = json.load(fo)
File "C:\Users\Tanabata\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\Tanabata\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\Tanabata\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 342, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 3 column 1 (char 2893)
JSON文件itselft:http://www.mediafire.com/file/l3rzzbe0nbu1nlu/Sclass.json
謝謝先生,但我得到了另一個錯誤:AttributeError:'_csv.writer'對象沒有屬性'寫' – Tanabata
對不起,應該是'writerow'。答案已更正。 – Daniel
謝謝先生,但另一個錯誤:作家()只需要一個參數(給定8)sry打擾你先生。 – Tanabata