-2
t = ["3.4","7","","-1.3","-5",""]

How to convert this list into integer and float for further ananlysis?

I have done this so far but i got error.

t = ["3.4","7","","-1.3","-5",""]

def integer_float(string):
    try:
        return int(string)
    except ValueError:
        return float(string)

for i in t:
    integer_float(i)

I got the error

ValueError: could not convert string to float:
Fabricator
  • 12,722
  • 2
  • 27
  • 40
  • 4
    What do you expect to be the result of converting `""` to a number? – mkrieger1 Jun 15 '17 at 23:30
  • Actually i imported that from a txt file and after importing everything becomes a string and there are some empty row as well. How to analyse such data. please help – Suman Shrestha Jun 15 '17 at 23:40

1 Answers1

1

because you can't parse empty string as int nor float. try:

int(string or "0")
Fabricator
  • 12,722
  • 2
  • 27
  • 40