Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List to float
#1
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.
Reply
#2
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)))
Reply
#3
(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
Reply
#4
#!/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
Reply
#5
(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.
Reply
#6
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)
Reply
#7
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  convert string to float in list jacklee26 6 1,818 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,763 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: list indices must be integers or slices, not float hissonrr 2 3,274 Apr-19-2020, 12:02 AM
Last Post: hissonrr
  Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key firebird 2 3,326 Jul-25-2019, 11:32 PM
Last Post: scidam
  Unable to print the exact Float values when I convert LIST to Sequence of Tuples? preethamalluri 1 2,384 Jul-12-2018, 09:03 AM
Last Post: buran
  TypeError: list indices must be integers or slices, not float RedSkeleton007 1 16,672 Feb-28-2018, 12:52 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020