I have a .svg file with example contents: <svg style="fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.001" /></svg>
I now want to use Python to directly edit the .svg file and change
style="fill:#000000; to my desired color and save it., but I am not sure how to go about this, I have tried a lot of libraries but none do what I
need.
Try this: https://pythonexamples.org/python-replace-string-in-file/
#read input file
fin = open("data.svg", "rt")
#read file contents to string
data = fin.read()
#replace all occurrences of the required string
data = data.replace('style="fill:#000000;', 'style="fill:#FF0000;')
#close the input file
fin.close()
#open the input file in write mode
fin = open("data.svg", "wt")
#overrite the input file with the resulting data
fin.write(data)
#close the file
fin.close()
Related
I have an csv file in my source folder, want to get the output as new line where we have "\r"
Source File
\name\age\gender\r\kiran\29\male\r\rekha\12\female\r\siva\39\male\r
Expected output file
\name\age\gender
\kiran\29\male
\rekha\12\female
\siva\39\male
with open('filename.csv', 'r+') as file:
data = file.readlines()[0].replace(('\\r\\','\n\\'))[:-1]
print(data)
I have 1000 files, and the name of these are "numbers", for example, 2323.csv.
I have these name in a file called 1.txt.
Now I want to open these files one by one in python, using 1.txt to open them.
How can I do this?
Why not this?
with open('1.txt', 'r') as listFile:
for line in listFile:
with open(line.rstrip(), 'r') as individualFile:
# do stuff
Roughly and very basic but understandable code (no error handling).
with open('1.txt', 'r') as f:
for line in f.readlines(): # This assumes each line has a number
with open('.'.join([line, 'csv']) as cf:
file_content = cf.readlines()
print(file_content)
I have written a program in python that does the following:
write an initial header in a new file
merge the files in the new file(ie append file to the new file, I want all my log files to be put together)
finally convert the space seperated file to csv.
What I do is mention the output directory where my file should be, and also a filelist,which contains the location of each file that should be merged, it looks like this:
/Users/ra/Documents/Dryad01/meow.log
/Users/ra/Documents/Dryad01/meow1.log
Then I do python program.py path_to_list_file output_dir
Here is my program :
import csv
def main():
parser = argparse.ArgumentParser()
parser.add_argument("filelist", help="Format: Value File in each line")
parser.add_argument("output_dir", help="output directory")
args = parser.parse_args()
# write header
fout = open(args.output_dir+"merged.txt","a")
fout.write("timestamp type response_time")
#from each file get the data and put it in fout/merge
with open(args.filelist) as f:
for file in f:
file_read = open(file)
for line in file_read:
fout.write(line)
fout.close()
#now all file in filelist have been merged
#next make them into csv files
make_csv(args.output_dir+"merged.txt",args.output_dir+"merged_csv.csv")
def make_csv(file1,file2):
with open(file1) as fin, open(file2, 'w') as fout:
o=csv.writer(fout)
for line in fin:
o.writerow(line.split())
But for some reason I get no error, no warning,but just no file!
What do you think is wrong?
Did you maybe forget the following main pattern at the end of your source file?
if __name__ == '__main__':
main()
If so, your main() function will never be called.
The context is the following one, I have two text file that I need to edit.
I open the first text file read it line by line and edit it but sometimes when I encounter a specific line in the first text file I need to overwritte content of the the second file.
However, each time I re-open the second text file instead of overwritting its content the below code appends it to the file...
Thanks in advance.
def edit_custom_class(custom_class_path, my_message):
with open(custom_class_path, "r+") as file:
file.seek(0)
for line in file:
if(some_condition):
file.write(mu_message)
def process_file(file_path):
with open(file_path, "r+") as file:
for line in file:
if(some_condition):
edit_custom_class(custom_class_path, my_message)
In my opinion, simultaneously reading and modifying a file is a bad thing to do. Consider using something like this. First read the file, make modifications, and then overwrite the file completely.
def modify(path):
out = []
f = open(path)
for line in f:
if some_condition:
out.append(edited_line) #make sure it has a \n at the end
else:
out.append(original_line)
f.close()
with open(path,'w') as f:
for line in out:
f.write(line)
I need some help Im trying to display the text files contents (foobar) with this code
text = open('C:\\Users\\Imran\\Desktop\\text.txt',"a")
rgb = text.write("foobar\n")
print (rgb)
text.close()
for some reason it keeps displaying a number. If anyone could help that would be awesome, thanks in advance
EDIT: I am Working with Python 3.3.
Print the contents of the file like this:
with open(filename) as f:
for line in f:
print(line)
Use with to ensure that the file handle will be closed when you are finished with it.
Append to the file like this:
with open(filename, 'a') as f:
f.write('some text')
# Open a file
fo = open("foo.txt", "r+")
str = fo.read();
print "Read String is : ", str
# Close opend file
fo.close()
More: http://www.tutorialspoint.com/python/python_files_io.htm
You are printing the number of written bytes. That won't work. Also you might need to open the file as RW.
Code:
text = open('...', "a")
text.write("foo\n")
text = open('...', "r")
print text.read()
If you want to display the contents of the file open it in read mode
f=open("PATH_TO_FILE", 'r')
And then print the contents of file using
for line in f:
print(line) # In Python3.
And yes, don't forget to close the file pointer f.close() after you finish the reading