#๐Ÿ”’ loading json file into python using 2 different file types

28 messages ยท Page 1 of 1 (latest)

green schooner
#

currently making a project where i just need to load in a json file, but for whatever reason whenever i execute the same 2 pieces of code into 2 different file extentions (in this case the FIRST screenshot is .py and the SECOND screenshot is .ipynb (jupyter notebook)

why does it work on the jupyter one but not the .py one?
screenshot 3 is the contents of the #gamingcaptions.json btw

frail fulcrumBOT
#

@green schooner

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

green schooner
#

essentially it just errors running this piece of code in a standard .py file but not .ipynb file. how can I fix this?

balmy mason
#

does your file's name really begin with a #? That's unusual.

green schooner
#

yeah could probs change that but it just makes sense for me personally

#

however if that alters the way python reads files then ill edit it

balmy mason
#

I don't understand the actual problem. You said it doesn't work, but what exactly is it doing, and how is that different from what you wanted?

green schooner
#

nono what i meant was

balmy mason
#

post your code as text, not screenshots

green schooner
#

in the jupyter notebook file, the code works

#

but in the .py file it doesnt

#

this is the code

with open('#gamingcaptions.json') as f:
    tags = list(json.load(f))
    print(len(tags))
balmy mason
#

ok you said it doesn't "work". So, again: what is it doing, and how is that different from what you expected?

green schooner
#

all its doing is just reading an empty list in a json file, but what is different to what i expected in the .py file is that in the .py file, i get a JSONDecodeError

but when i run that same code in a cell in jupyter notebook, it returns 0 as it should (as there is an empty list in the json file itself)

balmy mason
#

can you paste the text of the error?

green schooner
#

yea sure

#

Traceback (most recent call last):
File "c:\Users\a\Desktop\desktop stuff\nea files\tiktokscrape\alevelnea\nea.py", line 566, in <module>
web_scrape()
File "c:\Users\a\Desktop\desktop stuff\nea files\tiktokscrape\alevelnea\nea.py", line 532, in web_scrape
tags = list(json.load(f))
File "C:\Users\a\AppData\Local\Programs\Python\Python310\lib\json_init_.py", line 293, in load
return loads(fp.read(),
File "C:\Users\a\AppData\Local\Programs\Python\Python310\lib\json_init_.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\a\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\a\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

balmy mason
#

that means that the file you're reading is entirely empty.

#

I can't explain why it's empty, but it is.

green schooner
#

the thing is the file isnt actually empty, it only contains an empty list (which is '[]' )

woven flare
#

!rule 5 tiktok's ToS prohibits automated scraping. You can only use their official API for automated use

balmy mason
#

it's possible that you have two files with the same name -- one of which is empty, and the other of which has proper JSON in it.

frail fulcrumBOT
#

5. Do not provide or request help on projects that may violate terms of service, or that may be deemed inappropriate, malicious, or illegal.

green schooner
#

ah fair

#

yeah ahah that was the issue @balmy mason ty

frail fulcrumBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

๐Ÿ”’ loading json file into python using 2 different file types