Posts: 44
Threads: 17
Joined: Jan 2017
Hi guys,
I have a 3.1 Gb csv file which I try to clean of bad data. So, I wrote the code below:
#!/usr/bin/env python3
import csv
with open('teste.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
fh = open("saida.txt","a")
for row in readCSV:
if row[0] and row[1] != "":
print(row, file = fh)
fh.close() Here's the output file:
Output: ['1', '1', '1']
['1', '1', '1']
['1', '1', '1']
['1', '1', '1']
['1', '1']
['1', '1']
['1', '1', '1']
It works fine, but as I discovered later, I wrote a txt file with a "List" type. That cause the inconvenience to print " [ ] ' ' " characters on the txt file. I need a way to convert this list in my txt file to float, or even change the code to print float instead list.
I'm new on python, so I appreciate the help.
Posts: 8,160
Threads: 160
Joined: Sep 2016
Jan-16-2017, 03:07 PM
(This post was last modified: Jan-16-2017, 04:31 PM by buran.)
It's not that you entered a list, the csv reader returns each row as list of str objects.
from the docs for csv.reader:
Quote:Each row read from the csv file is returned as a list of strings. No automatic data type conversion is performed.
What shall you do, depends what you want to do with the data from the row
here is how you can print or convert to a list of floats
>>> row = ['1', '2', '0']
>>> print ', '.join(row)
1, 2, 0
>>> print map(float, row)
[1.0, 2.0, 0.0]
Just realised that you write the rows to second file. You should use with context manager for both files:
#!/usr/bin/env python3
import csv
with open('teste.csv') as csvfile, open("saida.txt","a") as fh:
readCSV = csv.reader(csvfile, delimiter=',')
if row[0] and row[1]:
fh.write('{}\n'.format(','.join(row)))
Posts: 44
Threads: 17
Joined: Jan 2017
(Jan-16-2017, 03:07 PM)buran Wrote: It's not that you entered a list, the csv reader returns each row as list of str objects.
from the docs for csv.reader:
Quote:Each row read from the csv file is returned as a list of strings. No automatic data type conversion is performed.
What shall you do, depends what you want to do with the data from the row
here is how you can print or convert to a list of floats
>>> row = ['1', '2', '0']
>>> print ', '.join(row)
1, 2, 0
>>> print map(float, row)
[1.0, 2.0, 0.0]
Just realised that you write the rows to second file. You should use with contects manager for both files:
#!/usr/bin/env python3
import csv
with open('teste.csv') as csvfile, open("saida.txt","a") as fh:
readCSV = csv.reader(csvfile, delimiter=',')
if row[0] and row[1]:
fh.write('{}\n'.format(','.join(row)))
Thanks for the help !!
I just added "for row in readCSV:" before "if row[0] and row[1]:", and I changed the comma in " ','.join(row))" for tabulation and worked perfect to me.
#!/usr/bin/env python3
import csv
with open('teste.csv') as csvfile, open("saida.txt","a") as fh:
readCSV = csv.reader(csvfile, delimiter=',')
for row in readCSV:
if row[0] and row[1]:
fh.write('{}\n'.format(' '.join(row))) Now I have a file with numbers and tabulation that's the best to my use:
Output: 1 1 1
1 1 1
1 1 1
1 1 1
1 1
1 1
1 1 1
Posts: 8,160
Threads: 160
Joined: Sep 2016
Jan-16-2017, 04:34 PM
(This post was last modified: Jan-16-2017, 04:35 PM by buran.)
#!/usr/bin/env python3
import csv
with open('teste.csv') as csvfile, open("saida.txt","a") as fh:
readCSV = csv.reader(csvfile, delimiter=',')
for row in readCSV:
if row[0] and row[1]:
fh.write('{}\n'.format('\t'.join(row))) you should use \t for TAB
Posts: 44
Threads: 17
Joined: Jan 2017
(Jan-16-2017, 04:34 PM)buran Wrote: #!/usr/bin/env python3
import csv
with open('teste.csv') as csvfile, open("saida.txt","a") as fh:
readCSV = csv.reader(csvfile, delimiter=',')
for row in readCSV:
if row[0] and row[1]:
fh.write('{}\n'.format('\t'.join(row))) you should use \t for TAB
Ok, thanks for the tip.
Posts: 7,319
Threads: 123
Joined: Sep 2016
An alternative is that there also a write csv object.
csv.writer(csv_out, delimiter='\t')
So here you can set tab before writing.
import csv
with open('test.csv') as csvfile, open("saida.txt","w") as fh:
read_csv = csv.reader(csvfile, delimiter=',')
write_csv = csv.writer(fh, delimiter='\t')
for row in read_csv:
if row[0] and row[1]:
write_csv.writerow(row)
Posts: 44
Threads: 17
Joined: Jan 2017
(Jan-16-2017, 09:06 PM)snippsat Wrote: An alternative is that there also a write csv object.
csv.writer(csv_out, delimiter='\t')
So here you can set tab before writing.
import csv
with open('test.csv') as csvfile, open("saida.txt","w") as fh:
read_csv = csv.reader(csvfile, delimiter=',')
write_csv = csv.writer(fh, delimiter='\t')
for row in read_csv:
if row[0] and row[1]:
write_csv.writerow(row)
Thanks !! That's a nice way too.
|