1. In the original code that OP show, they already read the whole json file in memory. So file is at the end. If you try to add your code at that point - there is nothing to iterate over. So your loop will not execute at all.
2. if you ignore OP reading the whole file in memory, iterating over file will yield one char at a time and you will get AttributeError
You will get the same error, even if you managed to load json properly and get some iterator that yield other objects, not char. I guess you used something like this to iterate over objects that had
All that said, json.dumps will produce str, so no need to cast to str explicitly. and if you write item by item (i.e. assuming item has text propery etc.) the resulting file will not be valid json.
I must also say that OP attempt to append JSON file to already existing file will not produce valid JSON either.
2. if you ignore OP reading the whole file in memory, iterating over file will yield one char at a time and you will get AttributeError
AttributeError: 'str' object has no attribute 'text'
You will get the same error, even if you managed to load json properly and get some iterator that yield other objects, not char. I guess you used something like this to iterate over objects that had
.text
property like html tags from BeautifulSoup. It's very likely that even if the object has text property you will get error when pass it to json.dumps.All that said, json.dumps will produce str, so no need to cast to str explicitly. and if you write item by item (i.e. assuming item has text propery etc.) the resulting file will not be valid json.
I must also say that OP attempt to append JSON file to already existing file will not produce valid JSON either.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs