Kickstarter JSON数据解析,保存csv

时间:2023-02-23 08:56:08


#!/usr/bin/python
# -*- encoding: utf-8 -*-
import json
import csv

if __name__ == '__main__':
file_object = open("Kickstarter_2019-01-17T03_20_02_630Z.json",'rb')
all_the_text = file_object.read() #结果为str类型
print (len(all_the_text))
#print ("all_the_text=",all_the_text)
file_object.close()

log_path = 'result.csv'
file = open(log_path, 'a+', encoding='utf-8', newline='')
csv_writer = csv.writer(file)
csv_writer.writerow(['backers_count',
'blurb',
'category',
'converted_pledged_amount',
'country',
'country_displayable_name',
'created_at',
'creator',
'currency',
'currency_symbol',
'currency_trailing_code',
'current_currency',
'deadline',
'disable_communication',
'friends',
'fx_rate',
'goal',
'id',
'is_backing',
'is_starrable',
'is_starred',
'launched_at',
'location',
'name',
'permissions',
'photo',
'pledged',
'profile',
'slug',
'source_url',
'spotlight',
'staff_pick',
'state',
'state_changed_at',
'static_usd_rate',
'urls',
'usd_pledged',
'usd_type'])

begin_index = all_the_text.find("{\"table_id\"".encode())
while begin_index != -1:
end_index = all_the_text.find("{\"table_id\"".encode(), begin_index + 1)
if -1 == end_index:
per_json = all_the_text[begin_index : ]
else:
per_json = all_the_text[begin_index : end_index]


per_json.replace(b'\n', b'')
print(per_json)
print('\n\n')

obj_json = json.loads(per_json)
obj_data = obj_json.get('data')
backers_count = obj_data.get('backers_count')
blurb = obj_data.get('blurb')
category = obj_data.get('category')
category = json.dumps(category)
converted_pledged_amount = obj_data.get('converted_pledged_amount')
country = obj_data.get('country')
country_displayable_name = obj_data.get('') #国家的全称,上面是缩写
created_at = obj_data.get('created_at')
creator = obj_data.get('creator')
creator = json.dumps(creator)
currency = obj_data.get('currency')
currency_symbol = obj_data.get('currency_symbol')
currency_trailing_code = obj_data.get('currency_trailing_code') #是否要转大写
current_currency = obj_data.get('current_currency')
deadline = obj_data.get('deadline')
disable_communication = obj_data.get('disable_communication')
friends = obj_data.get('friends') #可能没有这个值
fx_rate = obj_data.get('fx_rate')
goal = obj_data.get('goal')
id = obj_data.get('id')
is_backing = obj_data.get('is_backing') #可能没有这个值
is_starrable = obj_data.get('is_starrable') #是否要转大写 .upper()
is_starred = obj_data.get('is_starred') #是否要转大写 .upper()
launched_at = obj_data.get('launched_at')
location = obj_data.get('location')
location = json.dumps('location')
name = obj_data.get('name')
permissions = obj_data.get('permissions')
photo = obj_data.get('photo')
photo = json.dumps('photo')
pledged = obj_data.get('pledged')
profile = obj_data.get('profile')
profile = json.dumps('profile')

slug = obj_data.get('slug')
source_url = obj_data.get('source_url')
spotlight = obj_data.get('spotlight')
staff_pick = obj_data.get('staff_pick')
state = obj_data.get('state')
state_changed_at = obj_data.get('state_changed_at')
static_usd_rate = obj_data.get('static_usd_rate')
urls = obj_data.get('urls')
urls = json.dumps('urls')
usd_pledged = obj_data.get('usd_pledged')
usd_type = obj_data.get('usd_type')
begin_index = end_index

csv_writer.writerow([backers_count,
blurb,
category,
converted_pledged_amount,
country,
country_displayable_name,
created_at,
creator,
currency,
currency_symbol,
currency_trailing_code,
current_currency,
deadline,
disable_communication,
friends,
fx_rate,
goal,
id,
is_backing,
is_starrable,
is_starred,
launched_at,
location,
name,
permissions,
photo,
pledged,
profile,
slug,
source_url,
spotlight,
staff_pick,
state,
state_changed_at,
static_usd_rate,
urls,
usd_pledged,
usd_type])



# with open(CVS_FILE_NAME, 'wb') as csvfile:
# spamwriter = csv.writer(csvfile,dialect='excel')
# spamwriter.writerow([u'序号'.encode('gbk'), u'姓名'.encode('gbk'), u'电话'.encode('gbk'), u'访问时间'.encode('gbk'), u'IP位置'.encode('gbk'), u'渠道名称'.encode('gbk')])
# for item in listRes:
# spamwriter.writerow([str(item['num']), item['name'].encode('gbk'), item['tel'], item['createdate'], item['iplocation'].encode('gbk'), item['channel'].encode('gbk')])

print('\n\n end-------------------------------------------------')
file.close()