Json file parameter on Pycharm - python

I'm running twitter_hashtag_frequency.py program on Pycharm with json file parameter and I have this error :
['C:/Users/HP/PycharmProjects/Bonzanini_Book_Exercises/twitter_hashtag_frequency.py', 'stream_.jsonl']
Traceback (most recent call last):
File "C:/Users/HP/PycharmProjects/Bonzanini_Book_Exercises/twitter_hashtag_frequency.py", line 17, in <module>
tweet = json.loads(line)
File "C:\Users\HP\Anaconda3\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\HP\Anaconda3\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\HP\Anaconda3\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 1)
This is the code:
# Chap02-03/twitter_hashtag_frequency.py
import sys
from collections import Counter
import json
def get_hashtags(tweet):
entities = tweet.get('entities', {})
hashtags = entities.get('hashtags', [])
return [tag['text'].lower() for tag in hashtags]
if __name__ == '__main__':
print(sys.argv)
fname = sys.argv[1]
with open(fname, 'r') as f:
hashtags = Counter()
for line in f:
tweet = json.loads(line)
hashtags_in_tweet = get_hashtags(tweet)
hashtags.update(hashtags_in_tweet)
for tag, count in hashtags.most_common(20):
print("{}: {}".format(tag, count))
As you see in the screenshot the json file stream_.jsonlis in the same path with the program Pycharm running window
The windows of edit configuration sounds like good this is the screenshoot Run debug configurations screenshot
The json file file is an output of streaming tweets of 4629 lines. I would have your help, thank you.

Related

Json file cant be edited

I want to append the brightness 0 to all lamps in list to a empty json file.
def first_start(lamps:list):
for lamp in lamps:
dict = {"light": {lamp: {"brightness": 0}}}
with open("data.json", "r") as file:
data = json.load(file)
data.update(dict)
print(dict)
with open("data.json", 'w') as file:
json.dump(data, file)
Everytime I run this code, I get this error:
Traceback (most recent call last):
File "C:\Users\brend\PycharmProjects\Hue_Control\main.py", line 27, in <module>
first_start(lamps)
File "C:\Users\brend\PycharmProjects\Hue_Control\main.py", line 16, in first_start
data = json.load(file)
File "C:\Users\brend\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "C:\Users\brend\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\brend\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\brend\AppData\Local\Programs\Python\Python39\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)
Could somebody please help me, what im doing wrong?
Thank you :)
There is no such thing as an empty json file. If you have an empty file, then its not a json file.
You could ignore all the errors and assume that the file should contain "{}" and carry on:
def first_start(lamps:list):
for lamp in lamps:
upd = {"light": {lamp: {"brightness": 0}}}
data = {} # Empty dict just in case
try:
with open("data.json", "r") as file:
data = json.load(file)
except Exception:
print('Ignore errors with data.json')
data.update(upd)
with open("data.json", 'w') as file:
json.dump(data, file)

google translate sudently stopped working i didint change the code any way

Google translate libary stopped working.
I didn't change anything about my code and it worked like an hour ago. I don't understand what is happening.
Here is my code:
from googletrans import Translator
import time
K = Translator()
def anna_sanat(x):
K = Translator()
a = K.translate(x,dest="sv",src="en").text
b = K.translate(x,dest="fi",src="en").text
return [x,a,b]
f = open("sanat.txt",mode="r",encoding="utf-8")
g = f.read()
f.close()
g = g.split(" ")
h = []
for i in g:
h += i.split("\n")
for i in h:
print(i)
print(anna_sanat(i.replace("\"","")))
time.sleep(1)
and here is the error message:
Magnus
Traceback (most recent call last):
File "C:\Users\Taavi\Desktop\Koodaus\uusin Python\ruotsi\ruotsipeli.py", line 21, in <module>
print(anna_sanat(i.replace("\"","")))
File "C:\Users\Taavi\Desktop\Koodaus\uusin Python\ruotsi\ruotsipeli.py", line 7, in anna_sanat
a = K.translate(x,dest="sv",src="en").text
File "C:\Users\Taavi\AppData\Local\Programs\Python\Python37-32\lib\site-packages\googletrans\client.py", line 172, in translate
data = self._translate(text, dest, src)
File "C:\Users\Taavi\AppData\Local\Programs\Python\Python37-32\lib\site-packages\googletrans\client.py", line 81, in _translate
data = utils.format_json(r.text)
File "C:\Users\Taavi\AppData\Local\Programs\Python\Python37-32\lib\site-packages\googletrans\utils.py", line 62, in format_json
converted = legacy_format_json(original)
File "C:\Users\Taavi\AppData\Local\Programs\Python\Python37-32\lib\site-packages\googletrans\utils.py", line 54, in legacy_format_json
converted = json.loads(text)
File "C:\Users\Taavi\AppData\Local\Programs\Python\Python37-32\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\Taavi\AppData\Local\Programs\Python\Python37-32\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Taavi\AppData\Local\Programs\Python\Python37-32\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)
Oh I know this one, I think this happens because googletrans makes requests to google translate directly, and after a while, google translate starts returning nothing, the only way that I know to get around this is to add a time.sleep(*Large number here*) and test again after that.

Python JSON Loading file errors

I'm trying to load a file to a variable, but I get the error json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) in whitelist = json.load(f), what am I doing wrong?
def load_whitelist():
global whitelist
wl = "C:/Users/Administrator/Desktop/Software/whitelist.json"
if os.path.isfile(wl):
with open(wl, mode='r') as f:
whitelist = json.load(f)
f.close()
print(whitelist)
def save_whitelist():
wl = "C:/Users/Administrator/Desktop/Software/whitelist.json"
if os.path.isfile(wl):
with open(wl, mode='w') as f:
json.dump(whitelist, f, sort_keys=False)
f.close()
Full Traceback:
Traceback (most recent call last):
File "PC200.py", line 970, in <module>
load_whitelist()
File "PC200.py", line 51, in load_whitelist
whitelist = json.load(f)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x0000020022935828>
The JSON
[{"ignoresPlayerLimit": false, "name": "AndRKConnor8000","xuid":"2535435055474031"},{"ignoresPlayerLimit":false,"name":"ThePurplishGame","xuid":"2535461240132600"}]
Not really an answer but it cannot fit into a comment. With only the error message, it is impossible to know what happens. This error can be reproduced with an empty file, or with an incorrect encoding. Example simulating an empty file:
>>> import json
>>> import io
>>> fd = io.StringIO()
>>> wl = json.load(fd)
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
wl = json.load(fd)
File "D:\Program Files (x86)\Python37-32\lib\json\__init__.py", line 296, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "D:\Program Files (x86)\Python37-32\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "D:\Program Files (x86)\Python37-32\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "D:\Program Files (x86)\Python37-32\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)
Example simulating an UTF16 encoded file read as Latin1:
>>> t = '''[{"ignoresPlayerLimit": false, "name": "AndRKConnor8000","xuid":"2535435055474031"},{"ignoresPlayerLimit":false,"name":"ThePurplishGame","xuid":"2535461240132600"}]'''
>>> tt = t.encode('utf16').decode('latin1')
>>> fd=io.StringIO(tt)
>>> wl = json.load(fd)
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
wl = json.load(fd)
File "D:\Program Files (x86)\Python37-32\lib\json\__init__.py", line 296, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "D:\Program Files (x86)\Python37-32\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "D:\Program Files (x86)\Python37-32\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "D:\Program Files (x86)\Python37-32\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)
The only way to discriminate the problem is to read and print the content of the file:
with open(wl, mode='r') as f:
data = f.read() # read file conten
print(data) # display it as text
print([hex(ord(i)) for i in data]) # and as an hex dump
That way you will be sure of the way Python actually reads the file.
def load_whitelist():
global whitelist
wl = "C:/Users/Administrator/Desktop/Software/whitelist.json"
if os.path.isfile(wl):
with open(wl, mode='r') as f:
whitelist = json.load(f)
f.close()
def save_whitelist():
wl = "C:/Users/Administrator/Desktop/Software/whitelist.json"
print(whitelist)
with open(wl, mode='w') as f:
json.dump(whitelist, f, sort_keys=False)
f.close()
It seems like there was an error with saving the whitelist and it deleted the JSON contents, the code itself was ok (I removed the if os.path.isfile(wl): as it was unnecessary). Thank you all for trying to help!

Json Decode Error While loading multiple files from directory

import json
import pandas as pd
import collections
import os
path = '/home/vinay/hdfs/kafka-logs/event_tablenames.txt'
file_read = '/home/vinay/hdfs/kafka-logs/hdfs_events/'
table_file = '/home/hdfs/vinay/kafka-logs/events_tables/'
con_json = '/home/vinay/hdfs/kafka-logs/json_to_txt/'
os.chdir(file_read)
files=os.listdir('.')
for file in files:
line = file.split('.')[0]
with open(file ,'r') as f:
print (json.load(f))
data = json.load(f)
key = data[line]
od = collections.OrderedDict(sorted(key.items()))
df = pd.DataFrame(list(od.items()), columns=['col_name', 'type'])
df['col_name'].to_csv(con_json + line + '.txt',sep='\t', index=False, header=False)
Below is a full traceback:
Traceback (most recent call last):
File "events_match_hdfs.py", line 29, in <module>
data = json.load(f)
File "/home/vinay/anaconda3/lib/python3.5/json/__init__.py", line 268, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/home/vinay/anaconda3/lib/python3.5/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/home/vinay/anaconda3/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/home/vinay/anaconda3/lib/python3.5/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
You didn't show the full traceback from the error in your question, so this is a just a guess.
I think the problem is because you're trying to do json.load(f) twice on the same file. The first one consumes the entire file, so the second one fail. Try changing the first couple of lines after opening the file to this:
with open(file ,'r') as f:
data = json.load(f)
print(data)
Json filename was not proper,and the size was zero.Due to that it was
throwing error.
Thanks all for your inputs.

How do I give a JSON string object an attribute to read?

I'm trying to use JSON to select objects from a list, then delete those objects once they've been selected. I keep running into errors telling me
AttributeError: 'str' object has no attribute 'read'
path_album_list = 'C:\\Users\\steve\\AppData\\Local\\Programs\\Python\\Python36\\albums.json'
albums = json.load(path_album_list)
album = random.choice(albums)
print ('Today\'s soundtrack is "%s."' % album)
albums.remove(album)
json.dump(albums, path_album_list)
I also tried using json.loads, but then I got even more errors:
Traceback (most recent call last):
File "C:\Users\steve\AppData\Local\Programs\Python\Python36\randomizer.py", line 20, in <module>
albums = json.loads(path_album_list)
File "C:\Users\steve\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\steve\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\steve\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)*
json.load takes a file object, not a string. So you need to do this:
path_album_list = 'C:\\Users\\steve\\AppData\\Local\\Programs\\Python\\Python36\\albums.json'
jsonfile = open(path_album_list, "r")
albums = json.load(jsonfile)
jsonfile.close()
# ...
jsonfile = open(path_album_list, "w")
json.dump(albums, jsonfile)
jsonfile.close()
Or, you can use with statements, but the above is probably easier to understand.
path_album_list = 'C:\\Users\\steve\\AppData\\Local\\Programs\\Python\\Python36\\albums.json'
with open(path_album_list, "r") as jsonfile:
albums = json.load(jsonfile)
# ...
with open(path_album_list, "w") as jsonfile:
json.dump(albums, jsonfile)
The reason you need to open the files is that json interacts with files, not with strings of the file path.

Categories