I have a text file with the following contents:
3,3,5,7,9,10,1
I can read the file and reach the content by ReadToEnd() method, but I want to delete only '3', so that it has the contents:
5,7,9,10,10
How can I do this?
If you want to delete it from with in Notepad, try find & replacing 3, with and empty space. If you are doing it from within you program you have to parse the file and make a new one only appending it with the values you want
Related
I tried to prepend a file using data from another file. But unfortunately I cant find a way to prepend the existing file by using the data from another file, so now I'm creating a 3rd file(test) by appending data from both files in the correct order as below:
import os
if os.path.exists("test3.csv"):
os.remove("test3.csv")
with open("test.csv") as f1:
with open("test 2.csv") as f2:
with open("test3.csv","a") as f3:
for line in f2:
f3.write(line)
for line in f1:
f3.write(line)
Is it possible to achieve the same without the creation of a new file?
I have a CSV file with two columns in it, the one of the left being an old string, and the one directly to right being the new one. I have a heap of .xml files that contain the old strings, which I need to replace/update with the new ones.
The script is supposed to open each .xml file one at a time and replace all of the old strings in the CSV file with the new ones. I have tried to use a replace function to replace instances of the old string, called 'column[0]' with the new string, called 'column[1]'. However I must be missing something as this seems to do nothing. If I the first variable in the replace function to an actual string with quotation marks, the replace function works. However if both the terms in the replace function are variables, it doesn't.
Does anyone know what I am doing wrong?
import os
import csv
with open('csv.csv') as csv:
lines = csv.readline()
column = lines.split(',')
fileNames=[f for f in os.listdir('.') if f.endswith('.xml')]
for f in fileNames:
x=open(f).read()
x=x.replace(column[0],column[1])
print(x)
Example of CSV file:
oldstring1,newstring1
oldstring2,newstring2
Example of .xml file:
Word words words oldstring1 words words words oldstring2
What I want in the new .xml files:
Word words words newstring1 words words words newstring2
The problem over here is you are treating the csv file as normal text file not looping over the all the lines in the csv file.
You need to read file using csv reader
Following code will work for your task
import os
import csv
with open('csv.csv') as csvfile:
reader = csv.reader(csvfile)
fileNames=[f for f in os.listdir('.') if f.endswith('.xml')]
for f in fileNames:
x=open(f).read()
for row in reader:
x=x.replace(row[0],row[1])
print(x)
It looks like this is better done using sed. However.
If we want to use Python, it seems to me that what you want to do is best achieved
reading all the obsolete - replacements pairs and store them in a list of lists,
have a loop over the .xml files, as specified on the command line, using the handy fileinput module, specifying that we want to operate in line and that we want to keep around the backup files,
for every line in each of the .xml s operate all the replacements,
put back the modified line in the original file (using simply a print, thanks to fileinput's magic) (end='' because we don't want to strip each line to preserve eventual white space).
import fileinput
import sys
old_new = [line.strip().split(',') for line in open('csv.csv')]
for line in fileinput.input(sys.argv[1:], inplace=True, backup='.bak'):
for old, new in old_new:
line = line.replace(old, new)
print(line, end='')
If you save the code in replace.py, you will execute it like this
$ python3 replace.py *.xml subdir/*.xml another_one/a_single.xml
I have stored txt. file contents in a list like, given below then I am
trying to store it in new.txt file , file writing is working fine but
instead of text I am getting junk code , How can i get rid of this Please
help
ListOfLines =['ÿþGroup:Test\n', '\n', 'Fields:³TESTNO³TESTNUM³\n', '\n',
'³37³DUCK³DAFFY³³³³2']
fileName= open("path"+'//'+'TestFile+'.txt',"w+")
for item in ListOfLines :
x=(''.join([str(item)]))
fileName.write(x)
OutPut
片畯㩰䅐䥔久ൔഊ䘊敩摬㩳傳呁低䲳十乔䵁덅䥆乒䵁덅䥍乄䵁덅䥔䱔덅啓䙆塉傳呁䑉傳呁䥂呒덈䅐協塅亳䵁ㅅ傳呁䑉댱䍏啃䅐더䱈䰷䍏덋䅐䡔卉더䅐䍔䵏䆳䑄䕒卓䖳䡔䥎덃䥍剌
Set Encoding='UTF-8' before reading this file. It will work
I have successfully downloaded my data from a given url and for storing it into a csv file I used the following code:
fx = open(destination_url, "w") #write data into a file
for line in lines: #loop through the string
fx.write(line + "\n")
fx.close() # close the file object
return
What happened is that the data is stored but not in separate lines. As one can see in the snapshot - the data is not separated into a different lines when I use the '\n'.
Every separate line of data that I wanted seems to be separated via the '\r' (marked by yellow) on the same cell in the csv file. Here is a snip: .
I know I am missing something here but can I get some pointers with regards to rearranging each line that ends with a \r into a separate line ?
I hope I have made myself clear.
Thanks
~V
There is a method call writelines
https://www.tutorialspoint.com/python/file_writelines.htm
some example is in the given link you can try that first in reality it should work we need the format of the data (what is inside the element) during each iteration print that out if the above method does not work
I want to build up a table of contents file based on the comments from the first line of each file.
I can get to the files no problem and read the contents, but that only returns a buffer of the file.
I want the check if the first line is comments. if it is then extract that line and add it to a new file.
var bufferContents = through.obj(function(file,enc,cb){
console.log(file.contents);
});
If the file is pretty large, I would recommend use lazy module.
https://github.com/jpommerening/node-lazystream
Or if line length is specified, you can set chunk size.
https://stackoverflow.com/a/19426486/1502019