Python Forum

Full Version: TypeError: string indices must be integer
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi all,

am trying to convert a JSON file to a CSV file with a python script and in some cases it works but in others I get the message in the subject.
this is the script:
import json
import os
import csv
from os import listdir
from os.path import isfile, join

imp_file_loc = "C:\\Users\\XXX"

os.chdir(imp_file_loc)

for json_file_name in listdir(imp_file_loc):
	os.chdir(imp_file_loc)
	print(json_file_name)
	#json_file_name = "es_28-09-2018.json"

	csv_file_name = json_file_name.replace("json", "csv")
	print(csv_file_name)
	with open(json_file_name) as f:
		reviews_parsed = json.load(f)


	print(len(reviews_parsed))
		
	# open a file for writing
	out_file_loc = "C:\\Users\\YYY"

	os.chdir(out_file_loc)

	review_data = open(csv_file_name, 'wb')

	# create the csv writer object

	csvwriter = csv.writer(review_data)

	header = ['asin','country','reviews-count','price','currency','url','name','review_header','review_text','review_rating','review_author','review_url' ]
	csvwriter.writerow(header)

	reviews_n = len(reviews_parsed)

	for rev in range(reviews_n):

		asin = reviews_parsed[rev]['asin'].encode('utf-8')
		country = reviews_parsed[rev]['country'].encode('utf-8')
		reviews_count = reviews_parsed[rev]['reviews-count'].encode('utf-8')
		price = reviews_parsed[rev]['price'].encode('utf-8')
		currency = reviews_parsed[rev]['currency'].encode('utf-8')
		url = reviews_parsed[rev]['url'].encode('utf-8')
		name = reviews_parsed[rev]['name'].encode('utf-8')

		
		reviews_page_n = len(reviews_parsed[rev]['reviews'])
	
		for rev_txt in range(reviews_page_n):

			try:
		

				review_header = reviews_parsed[rev]['reviews'][rev_txt]["review_header"].encode('utf-8')
				review_text = reviews_parsed[rev]['reviews'][rev_txt]["review_text"].encode('utf-8')
				review_rating = reviews_parsed[rev]['reviews'][rev_txt]["review_rating"].encode('utf-8')
				review_author = reviews_parsed[rev]['reviews'][rev_txt]["review_author"].encode('utf-8')
				review_url = reviews_parsed[rev]['reviews'][rev_txt]["review_url"].encode('utf-8')

				data = [asin,country,reviews_count,price,currency,url,name,review_header,review_text,review_rating,review_author,review_url]
				csvwriter.writerow(data)
				
			except:
				print(asin, rev, type(rev))
				print(rev_txt, type(rev_txt))
				print(reviews_parsed[273]["reviews"][0]["review_header"].encode('utf-8'))
				print(reviews_parsed[274]["reviews"][0]["review_header"].encode('utf-8'))				
				print('Error : ' + str(rev) + '-' + str(rev_txt))
				raise

	review_data.close()
In my case, it will crash in line 62
any ideas what to do to solve this....
Can you attach a sample json file, or tell us where we can download one?