I have configurations for python script which includes integers, strings and dictionaries. A sample config.txt file is as below
mode = 'train,test,validation'
pick_one_in_n_files = 2
#dictionary for labels
labels = dict(
bowl=0,
coffee_mug=1,
food_bad=2,
food_box=3,
food_can=4,
instant_noodles=5,
plate=6,
soda_can=7,
sponge=8,
water_bottle=9
)
I am reading this text file and writing a new temporary python file with same text as in config. Then import the new python file into my script and use data from it.
configuration_import = open(config_path.replace('txt','py'),mode = 'w+')
configuration_text = open(config_path,mode ='r')
configuration_import.write(configuration_text.read())
configuration_import.close()
configuration_text.close()
return importlib.import_module(config_path.replace('.txt',''))
This serves the purpose but i am looking for an elegant approach to this problem.
This way the user will only provide a configuration text file. He will not be allowed to edit python files. Drawback is that the file has to be in python format instead of some standard yaml, json etc.
I suppose just using with would clean it up a bit:
with open(config_path, mode = 'r') as file_in:
with open(config_path.replace('.txt', '.py'), mode = 'w') as file_out:
for line in file_in:
file_out.write(line)
return importlib.import_module(config_path.replace('.txt',''))
Or even just import the file directly without copying it at all.
Related
and thank you for taking the time to read this post. This is literally my first time trying to use Python so bare with me.
My Target/Goal: Edit the original text file (Original .txt file) so that for every domain listed an "OR" is added in between them (below target formatting image). Any help is greatly appreciated.
I have been able to google the information to open and read the txt file, however, I am not sure how to do the formatting part.
Script
Original .txt file
Target formatting
You can achieve this in a couple lines as:
with open(my_file) as fd:
result = fd.read().replace("\n", " OR ")
You could then write this to another file with:
with open(formatted_file, "w") as fd:
fd.write(result)
something you could do is the following
import re
# This opens the file in read mode
with open('Original.txt', 'r') as file:
# Read the contents of the file
contents = file.read()
# Seems that your original file has line breaks to each domain so
# you could replace it with the word "OR" using a regular expression
contents = re.sub(r'\n+', ' OR ', contents)
# Then you should open the file in write mode
with open('Original.txt', 'w') as file:
# and finally write the modified contents to the file
file.write(contents)
a suggestion is, maybe you want to try first writing in a different file to see if you are happy with the results (or do a copy of Original.txt just in case)
with open('AnotherOriginal.txt', 'w') as file:
file.write(contents)
I'm looking to edit a Minecraft Windows 10 level.dat file in python. I've tried using the package nbt and pyanvil but get the error OSError: Not a gzipped file. If I print open("level.dat", "rb").read() I get a lot of nonsensical data. It seems like it needs to be decoded somehow, but I don't know what decoding it needs. How can I open (and ideally edit) one of these files?
To read data just do :
from nbt import nbt
nbtfile = nbt.NBTFile("level.dat", 'rb')
print(nbtfile) # Here you should get a TAG_Compound('Data')
print(nbtfile["Data"].tag_info()) # Data came from the line above
for tag in nbtfile["Data"].tags: # This loop will show us each entry
print(tag.tag_info())
As for editing :
# Writing data (changing the difficulty value
nbtfile["Data"]["Difficulty"].value = 2
print(nbtfile["Data"]["Difficulty"].tag_info())
nbtfile.write_file("level.dat")
EDIT:
It looks like Mojang doesn't use the same formatting for Java and bedrock, as bedrock's level.dat file is stored in little endian format and uses non-compressed UTF-8.
As an alternative, Amulet-Nbt is supposed to be a Python library written in Cython for reading and editing NBT files (supposedly works with Bedrock too).
Nbtlib also seems to work, as long as you set byteorder="little when loading the file.
Let me know if u need more help...
You'll have to give the path either relative to the current working directory
path/to/file.dat
Or you can use the absolute path to the file
C:user/dir/path/to/file.dat
Read the data,replace the values and then write it
# Read in the file
with open('file.dat', 'r') as file :
filedata = file.read()
# Replace the target string
filedata = filedata.replace('yuor replacement or edit')
# Write the file out again
with open('file.dat', 'w') as file:
file.write(filedata)
I've tried using python's mailmerge library on MS Word fields, but I lose all the formatting on the text. I'm wondering if there's a way to replicate going in Word and using 'Insert>Object>Text from file' using a python library (or really anything at this point). If there's an easy way to do this by editing the oxml even that would work. I just need some idea of where to start looking, or if I need to program this by hand.
I tried to come up with something to start with, hope it is useful. In the code below, I do open the file or create one if it does not exist, write some lines of data. Read the same lines of data in file1 then write them in a freshly created file2.
#Read and write files using the built-in Python methods
def main():
#open the file for writing and create if it does not exist
file = open("file1.txt", "w+")
#write some lines of data to the file
for i in range(5):
file.write("This is Andela %d\r\n" % (i + 1))
file.close()
#write text in file1.txt to another file called file2
open("file2.txt", "w").writelines([l for l in open("file1.txt").readlines() if "Andela" in l])
main()
I am able to make the text file into a list and sort it alphabetically. I am having trouble saving/inserting this new list into a NEW text file called GirlsNamesSorted.txt. I currently have:
newGirlFile = open('/Users/MacUser/Documents/Python/GirlNamesSorted.txt')
for i in newGirlFile:
j = i.rstrip("\r\n")
girlList.append(j)
girlList.sort(key=str.lower)
newGirlFile.write("\n".join(girlList))
print newGirlFile.read()
newGirlFile.close()
I believe the error is coming from:
newGirlFile.write("\n".join(girlList))
But I am not entirely sure and need help finding the error and fixing it.
To sort your file, just do:
from contextlib import closing
with closing(open('/Users/MacUser/Documents/Python/GirlNamesSorted.txt', 'w+')) as newGirlFile:
girlList=sorted([i.strip() for i in newGirlFile], key=str.lower)
newGirlFile.write("\n".join(girlList))
Note: you should also use with syntax when writing and reading file so the file gets closed properly. In Jython, the context manager requires an additional closing()
By default, open() opens a file as read-only; you need to specify the mode, in this case w+ for writing and reading, as the second parameter.
newGirlFile = open('/Users/MacUser/Documents/Python/GirlNamesSorted.txt', 'w+')
It is not a good practice to read input from a file and write output to the same file.
Please crate new file to write the output as mentioned below,
newGirlFile = open('/Users/MacUser/Documents/Python/GirlNamesSorted.txt','r')
out_file = open('/Users/MacUser/Documents/Python/GirlNamesSortedOutput.txt', 'w')
girlList = []
for i in newGirlFile:
j = i.rstrip("\r\n")
girlList.append(j)
girlList.sort(key=str.lower)
out_file.write("\n".join(girlList))
newGirlFile.close()
out_file.close()
Hope this will works.
Please let me know in terms of any queries.
oldGirlFile = open('/Users/MacUser/Documents/Python/GirlNamesSorted.txt', 'r')
girlList = [line.rstrip("\r\n") for line in newGirlFile.readlines()]
oldGirlFile.close()
girlList = girlList.sort(key=str.lower)
newGirlFile = open('/Users/MacUser/Documents/Python/GirlNamesSortedNew.txt', 'w')
newGirlFile.write("\n".join(girlList))
newGirlFile.close()
Then you can open the file in your text editor and check its content.
Can we write on (Here it should be editting infact) & read the same csv file at the same time in Python using the csv library?
Can a csv file be opened for editing & appending?
If so, how?
Short answer: no
Long answer: it depends
Appending data is perfectly possible using the CSV writer. Just open the file in append "a" mode:
with file("data.csv", "a" as fh:
w = csvwriter(fh):
w.writerow(...)
Editing a CSV file is not that simple as you will need to insert and remove parts of the file unless the columns you are editing are fixed length. The csv module has no builtin method for this.
You can open the original file, remove (or rename the original file) and open a new file with the same name:
with file("data.csv", "r") as rfh:
os.remove("data.csv"):
r = csvreader(rfh)
with file("data.csv", "w") as wfh:
w = csvwriter(wfh)
# ... read from r and write to w
Under Linux the original file will stay available for reading until the point when it is closed, so you do not need to rename it beforehand. I'm not too familiar with windows so you might need to rename the original file before creating the new file and remove the old file after closing it.
Another important bit: You can read and write from/to the same file without any troubles if your writing is limited to appending data.
with file("data.csv", "r") as rfh, file("data.csv", "a") as wfh:
r = csvreader(rfh)
w = csvwriter(wfh)
# you can read using r and append using a
Just be careful - your reader will be able to read the lines that you have just written using the writer. Be careful that you do not end up in an infinite loop causing a very big file.