I'm reading content from a JSON file and appending it to a text file . I'm getting the following error :
' write() argument must be str, not generator ' when I run this code and I'm not able to correct it .
with open('stackExchangeAPI.json','r') as json_file:
tags_list = []
data = json.load(json_file)
for i in range(0,99):
for j in range(0,99):
tags = data[i]["items"][j]["tags"]
with open('tags.txt','a+') as tags_file:
tags_file.seek(0)
d = tags_file.read(100)
if len(d) > 0 :
tags_file.write("\n")
tags_file.write(f'{tags[i]}' for i in range(0,(len(tags)-1)))
The error is from the last line ' tags_file.write(f'......) '
Can someone please help me rectify this ?
As it says, you are trying to write
a generator, you must first convert it to a string, probably by using join
:
out = ''.join(f'{tags[i]}' for i in range(0,(len(tags)-1)))
tags_file.write(out)