I am trying to read in a 4 column txt file and create a 5th column. Column 1 is a string and columns 2-4 are numbers, however they are being read as strings. I have two questions - my python script is currently unable to perform multiplication on two of the columns because it is reading columns 2-4 as strings. I want to know how to change columns 2-4 (which are numbers) to floating numbers and then create a 5th column that is two of the previous columns multiplied together.
Asked
Active
Viewed 832 times
0
-
Please post your code and clearly explain what parts you find are not working. The more you can provide and explain, the better we can help. This is a good reference: http://stackoverflow.com/help/how-to-ask – idjaw Oct 06 '15 at 23:23
-
Possible duplicate of [Parse String to Float or Int](http://stackoverflow.com/questions/379906/parse-string-to-float-or-int) – TigerhawkT3 Oct 06 '15 at 23:24
3 Answers
0
Just cast them to float using float(x)
method where x is the string that contains the float

Gudbergur
- 2,684
- 2
- 16
- 5
0
Whenever you iterate over the Lines, you could run the string through a try-except operation like:
try:
float(value)
except ValueError:
pass
#Or do some kind of error handling here

Xeno
- 89
- 6