Date,Time,Ref,Sen,ATN,Flow,PCB temp,Status,Battery,BC
2015/04/23,12:30:00,779581,908043,"-15,254",49,31,0,100,
2015/04/23,12:35:00,778715,907084,"-15,259",49,31,0,100,-127
2015/04/23,12:40:00,778299,906419,"-15,239",49,32,0,100,461
(...)
Hi, I have an ascii file like the one above where I am trying to replace the commas for semicolon. This is the code I am using:
filein = open('Prueba1.txt')
fileout = open('Fin.txt', 'wt')
for line in filein:
if line.startswith('20'):
fileout.write( line.replace(',', ';') )
filein.close()
fileout.close()
The problem is that now I want to maintain the commas for the 5th column and also take of the quotation marks. Any ideas?
Have you considered saving this as a csv file.
after saving it as csv file:
output:
You can read this as a dataframe.
This is an answer making use of the
split()
method if you do not want to use .csv files.Let's consider the line to be:
line = '2015/04/23,12:35:00,778715,907084,"-15,259",49,31,0,100,-127'
First, split the line into three parts. One before the part in the quotes, the quoted part, and the part after the part in quotes. This can be done by
line.split('"')
.This will give us a list consisting of these three parts:
alist = ['2015/04/23,12:35:00,778715,907084,', '-15,259', ',49,31,0,100,-127']
Now, split the first and last elements of the list by the comma. And add all the elements to an empty string. Write this string to your new file.
Like this:
The final result should look like this:
'2015/04/23;12:35:00;778715;907084;-15,259;49;31;0;100;-127;'
A solution using the
csv
Python standard library:Quote from the documentation about the QUOTE_MINIMAL option:
The output file is: