Python Forum
Convert .csv string into float
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert .csv string into float
#1
Hi guys!

The file that results of a test I did is a .csv file, containing results only in str type.
How can I convert only a specific entire column of the .csv file into float?

Example that sums up my case:
ResultFile.csv (2 lines, 6 columns):
Output:
Hello,how,are,you,1,2 I,hope,you're,good,3,4
If I'd type the following command:
sample = open('path','r')
for line in sample:
   info = line.split(',')
   print (type(info[4]))
I'd like the result to be:
Output:
<type 'float'> <type 'float'>
How do I need to treat the .csv file to obtain that, while the type for all other columns remains str??????
Reply
#2
use float built-in function
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

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
  openpyxl convert data to float jacklee26 13 5,716 Nov-19-2022, 11:59 AM
Last Post: deanhystad
  how to convert tuple value into string mg24 2 2,237 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  Convert SQLite Fetchone() Result to float for Math Extra 13 3,390 Aug-02-2022, 01:12 PM
Last Post: deanhystad
  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
  Convert string to float problem vasik006 8 3,269 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Detecting float or int in a string Clunk_Head 15 4,282 May-26-2022, 11:39 PM
Last Post: Pedroski55
  Convert a string to a function mikepy 8 2,421 May-13-2022, 07:28 PM
Last Post: mikepy
Question How to convert string to variable? chatguy 5 2,231 Apr-12-2022, 08:31 PM
Last Post: buran
  Convert string to int Frankduc 8 2,395 Feb-13-2022, 04:50 PM
Last Post: menator01

Forum Jump:

User Panel Messages

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