This question already has answers here:
How do I put a variable’s value inside a string (interpolate it into the string)?
(9 answers)
Which is the preferred way to concatenate a string in Python? [duplicate]
(12 answers)
Closed 3 years ago.
I'm writing a script that works with certain vocabulary. As a part of it, I want to store the incoming json data of a word in a file. The following code works only for one word at a time. But it will replace the data.json file every time with the json information of the new word.
with open('data.json', 'w') as outfile:
json.dump(data, outfile)
What I'm trying to achieve is: I would like to store the json of each file separately.
For eg: if the word is "internet", then I want the file to export internet.json and if the word is "persistence," then i want the json to store persistence.json
I tried the below, but it throws a syntax error:.
with open(word||'.json', 'w') as outfile:
json.dump(data, outfile)
I'm new to Python (using Python3) and I'm working on a pet-project. So, it would be a great support if you can help me in achieving this. Thanks in advance.
Related
This question already has answers here:
How to add a key-value to JSON data retrieved from a file?
(3 answers)
Closed 6 months ago.
I have a folder containing ~10000 JSON files, and I'm looking to add a new identical key/value pair ("Symbol": "PPF") to each one.
Each JSON file is named using an increasing number: 0.json, 1.json, 2.json, ... 9999.json, 10000.json
My current best attempt was trying to remove the last line of every file (the final }), then append ,"Symbol": "PPF".
What would be the fastest way of adding that key/value pair to all JSON files?
Try the following looping over the files:
# Read a json file temp.json and convert to Python dict
import json
with open('temp.json') as f:
data = json.load(f)
data["key"] = "value"
# Write back to file
with open('temp.json', 'w') as f:
json.dump(data, f)
This question already has answers here:
Reading JSON from a file [duplicate]
(7 answers)
Closed 1 year ago.
I am trying to read a dictionary from a JSON file which would look something like this.
{"#C": "C:\\Users\\user\\examplefolder}
I would like to load the dictionary from the file into filepath_dict. I am currently using this method to try this however, it refuses to load anything that is not a string.
import json
with open("filepaths.json", "r") as file:
file.write(json.loads(filepaths_dict))
How can I load the JSON dictionary into a python one?
Use json.load
with open("filepaths.json", "r") as f:
filepaths_dict = json.load(f)
json.loads: Will take a string
json.load: Will take a file
Also be careful about using the word "file" as a variable, you are overriding the built-in file function.
This question already has answers here:
How do you read a file into a list in Python? [duplicate]
(8 answers)
Closed 3 years ago.
What i am trying to do is read from a file, and insert the content from a file into a list in python 3. the file should look like this:
♥A
♣A
♥Q
♠Q
and the result i am expecting when i read from the file is that when i print the specific list is that it shows up like this
['♥A', '♣A', '♥Q', '♠Q']
How would i go about solving this?
And i have tried multiple solutions for this, like using for loops, but i dont understand how to do this
You can use the open function.
f = open("file.txt", "r")
l = list()
for line in f:
l.append(line)
f.close()
print(l)
This question already has answers here:
Correct way to write line to file?
(17 answers)
Closed 5 years ago.
My code is like this .
inputfile=np.genfromtxt('test1.dat')
for data in inputfile:
lat=floor(data)+(floor(abs((data-floor(data))*100))/60)+....
print lat
In command window I can see
12.9579738889
12.9579736111
12.9579727778
12.9579719444
12.9579711111
12.9579702778
12.9579694444
.......
But I want to save it in a text file in my working directory .
I am not getting how to proceed. All attempts failed.
Please give suggestions.
Thanks in advance.
It's really simple. Use open to open the file (get a file object which encapsulates the file descriptor) and then simply write to that file. In a true Pythonic way you'll want to use a context manager (with open(..) as file), so the the file is closed automatically when out of context.
inputfile=np.genfromtxt('test1.dat')
with open("/path/to/output.file", "w") as f:
for data in inputfile:
lat=floor(data)+(floor(abs((data-floor(data))*100))/60)+....
f.write("%f\n" % lat)
You can do following. I used f-string to format output, which is available in Python >= 3.6, but you can you any version to do some calculation first and then output the value.
>>> with open('test1.dat') as f_in:
... with open('outputfile.txt', 'w') as f_out:
... for data in f_in:
... f_out.write(f"{floor(data)+(floor(abs((data-floor(data))*100))/60)}\n")
if your data has more than one values you can use split function:
lan, lot = (float(x) for x in data.split(' '))
where ' ' is your separator between those values
This question already has answers here:
Easiest way to read/write a file's content in Python
(7 answers)
Closed 6 years ago.
In Python, how to read a file content only (not including attribute and filename), like using InputStream in Java?
I need a method that works for various file formats
I've tried this
with open(filePath, "rb") as imageFile:
str = base64.b64encode(imageFile.read())
M=str.decode()
print(M)
The problem is, I will get error for any object after that block
The most basic way is like so:
with open("filename.txt") as f:
contents = f.read()
The variable contents will now contain a string of everything in the file. More information is in the Python Documentation (https://docs.python.org/3/tutorial/inputoutput.html).