I'm storing a dictionary on a JSON file so I can easily access and change the data between sessions. This is how I'm trying to load the file into a variable, but after "pc_data = " I'm getting 'Expression Expected' from PyCharm. How am I able to load the file into a variable and then continue the function after variable assignment? Code below:
pc_data = with open(lib_dir+'player_characters.txt', 'r') as json_data:
json.load(json_data)
yield json_data
Your use of with is incorrect. with does not return a value, so you cannot assign it.
Try this:
with open(lib_dir+'player_characters.txt', 'r') as json_data:
pc_data = json.load(json_data)
yield pc_data # <- I'm guessing you want to yield parsed json here?
Related
I'm trying to write a dictionary to a file.
and when I try to read it back, it returns nothing, as if the file is empty.
I saw the file and there is something written into it.
I think that the way I write is not the best and that's why it does the problem.
my expected outcome is to read the file, get back the dictionary, and write to it. This is how I write:
# this function gets the db_name, creating a text file with the db_name
# and creating a empty dictionary with the key that is the table_name.
def __createTheDb(self,dirPath, db_name, table_name):
global db_file
os.chdir(dirPath) #gets into the directory
print(os.getcwd(), "from create the db")
db_file = open(db_name + ".txt", "w")
temp_dict = dict.fromkeys(f"{table_name}".split())
db_file.write(str(temp_dict))
return db_file
How I write the file:
def writeFile(self, db_name, table_name, key, value):
global db_file, my_json #gets the global variable
print(type(db_file))
file = open(db_name,"r").read()
print(file) #-> the problem is that this prints nothing.
I tried to read it with json.load(db_file), but it says it's unreadable.
The problem ->
return loads(fp.read(),
io.UnsupportedOperation: not readable
I just want to convert the text file to dictionary so I can set it's key...
try loading it with
with open("./path/file.json", "r") as f:
loadedDic = json.load(f)
then loadedDic will be equal to the dictionary IF the file you loaded is just a dictionary. If it has a list of dictionaries, or a dictionary of dictionaries, etc you'll need to parse it the way you normally parse lists/dicts/etc.
I have created a JSON file after scraping data online with the following simplified code:
for item in range(items_to_scrape)
az_text = []
for n in range(first_web_page, last_web_page):
reviews_html = requests.get(page_link)
tree = fromstring(reviews_html.text)
page_link = base_url + str(n)
review_text_tags = tree.xpath(xpath_1)
for r_text in review_text_tags:
review_text = r_text.text
az_text.append(review_text)
az_reviews = {}
az_reviews[item] = az_text
with open('data.json', 'w') as outfile:
json.dump(az_reviews , outfile)
There might be a better way to create a JSON file with the first key equal to the item number and the second key equal to the list of reviews for that item, however I am currently stuck at opening the JSON file to see the items have been already scraped.
The structure of the JSON file looks like this:
{
"asin": "0439785960",
"reviews": [
"Don’t miss this one!",
"Came in great condition, one of my favorites in the HP series!",
"Don’t know how these books are so good and I’ve never read them until now. Whether you’ve watched the movies or not, read these books"
]
}
The unsuccessful attempt that seems to be closer to the solution is the following:
import json
from pprint import pprint
json_data = open('data.json', 'r').read()
json1_file = json.loads(json_data)
print(type(json1_file))
print(json1_file["asin"])
It returns a string that replicates exactly the result of the print() function I used during the scraping process to check what the JSON file was going to be look like, but I can't access the asins or reviews using json1_file["asin"] or json1_file["reviews"] since the file read is a string and not a dictionary.
TypeError: string indices must be integers
Using the json.load() function I still print the right content, but I have cannot figure out how to access the dictionary-like object from the JSON file to iterate through keys and values.
The following code prints the content of the file, but raises an error (AttributeError: '_io.TextIOWrapper' object has no attribute 'items') when I try to iterate through keys and values:
with open('data.json', 'r') as content:
print(json.load(content))
for key, value in content.items():
print(key, value)
What is wrong with the code above and what should be adjusted to load the file into a dictionary?
string indices must be integers
You're writing out the data as a string, not a dictionary. Remove the dumps, and only dump
with open('data.json', 'w') as outfile:
json.dump(az_reviews, outfile, indent=2, ensure_ascii=False)
what should be adjusted to load the file into a dictionary?
Once you're parsing a JSON object, and not a string, then nothing except maybe not using reads, then loads and rather only json.load
Another problem seems to be that you're overwriting the file on every loop iteration
Instead, you probably want to open one file then loop and write to it afterwards
data = {}
for item in range(items_to_scrape):
pass # add to data
# put all data in one file
with open('data.json', 'w') as f:
json.dump(data, f)
In this scenario, I suggest that you store the asin as a key, with the reviews as values
asin = "123456" # some scraped value
data[asin] = reviews
Or write a unique file for each scrape, which you then must loop over to read them all.
for item in range(items_to_scrape):
data = {}
# add to data
with open('data{}.json'.format(item), 'w') as f:
json.dump(data, f)
Scenario is i need to convert dictionary object as json and write to a file . New Dictionary objects would be sent on every write_to_file() method call and i have to append Json to the file .Following is the code
def write_to_file(self, dict=None):
f = open("/Users/xyz/Desktop/file.json", "w+")
if json.load(f)!= None:
data = json.load(f)
data.update(dict)
f = open("/Users/xyz/Desktop/file.json", "w+")
f.write(json.dumps(data))
else:
f = open("/Users/xyz/Desktop/file.json", "w+")
f.write(json.dumps(dict)
Getting this error "No JSON object could be decoded" and Json is not written to the file. Can anyone help ?
this looks overcomplex and highly buggy. Opening the file several times, in w+ mode, and reading it twice won't get you nowhere but will create an empty file that json won't be able to read.
I would test if the file exists, if so I'm reading it (else create an empty dict).
this default None argument makes no sense. You have to pass a dictionary or the update method won't work. Well, we can skip the update if the object is "falsy".
don't use dict as a variable name
in the end, overwrite the file with a new version of your data (w+ and r+ should be reserved to fixed size/binary files, not text/json/xml files)
Like this:
def write_to_file(self, new_data=None):
# define filename to avoid copy/paste
filename = "/Users/xyz/Desktop/file.json"
data = {} # in case the file doesn't exist yet
if os.path.exists(filename):
with open(filename) as f:
data = json.load(f)
# update data with new_data if non-None/empty
if new_data:
data.update(new_data)
# write the updated dictionary, create file if
# didn't exist
with open(filename,"w") as f:
json.dump(data,f)
I get this string from the web
'Probabilità'
and I save it in a variable called temp. Than I stored it in a dictionary
dict["key"]=temp
Then I need to write all the dictionary in a JSON file and I use this function
json_data = json.dumps(dict)
But when I look at the JSON file written by my code I see this
'Probabilit\u00e0'
How can I solve this encoding problem?
Specify the ensure_ascii argument in the json.dumps call:
mydict = {}
temp = "Probabilità"
mydict["key"] = temp
json_data = json.dumps(mydict, encoding="utf-8", ensure_ascii=False)
I am trying to read a json file from python script using the json module. After some googling I found the following code:
with open(json_folder+json) as json_file:
json_data = json.loads(json_file)
print(json_data)
Where json_folder+json are the path and the name of the json file. I am getting the following error:
str object has no attribute loads.
The code is using json as a variable name. It will shadow the module reference you imported. Use different name for the variable.
Beside that, the code is passing file object, while json.loads accept a string.
Pass a file content:
json_data = json.loads(json_file.read())
or use json.load which accepts file-like object.
json_data = json.load(json_file)
import json
f = open( "fileToOpen.json" , "rb" )
jsonObject = json.load(f)
f.close()
it should seems you are doing in rather complicated way.
Try like this :-
json_data=open(json_file)
data = json.load(json_data)
json_data.close()
Considering the path to your json file is set to the variable json_file:
import json
with open(json_file, "rb") as f:
json_data = json.load(f)
print json_data
I Make This....
import urllib2
link_json = "\\link-were\\"
link_open = urllib2.urlopen(link_json) ## Open and Return page.
link_read = link_open.read() ## Read contains of page.
json = eval(link_read)[0] ## Transform the string of read in link_read and return the primary dictionary ex: [{dict} <- return this] <- remove this
print(json['helloKey'])
Hello World