简体   繁体   中英

ValueError when reading from a file

I have a python script that reads items from a file, devices.txt and then populates a dictionary. Now this file has the following structure:

uniqueId,RoomLabel,Label,State,Frequency

Eg:

1001,lounge,lamp,False,6789

Now when python reads that alone, it's fine. However, when I populate the file further:

1001,lounge,lamp,False,5169,1002,lounge,tv,False,6789

It spits out:

unId = int(value[0])
ValueError: invalid literal for int() with base 10: ''

This is the code running at this point of the program:

with open("devices.txt") as file:
for line in file:
    value = line.rstrip('\n').split(",")
    unId = int(value[0])
    rmLabel = value[1]
    label = value[2]
    state = value[3]
    freq = int(value[4])
    irTable[unId] = rmLabel, label, state, freq

With that in mind, i'm failing to see where it's finding an int() with a base of 10, I'm awful at math, but as I understand, for it to be a base of 10 it would be seeing a decimal correct?

The error message ValueError: invalid literal for int() with base 10: '' means the following:

You asked Python to convert an object to an integer by calling int() .

Because you didn't specify a base, it assumes a default of 10 (decimal) (you could for example tell it to parse a number in hex by specifying a base of 16 : int('ff', 16) ). And the value that caused this exception was '' (the empty string). So the empty string '' cannot be converted to an integer using base 10 (or any other base in this case).

The exact line of code where this call to int() happened is printed just above the exception:

unId = int(value[0])

So you know that value[0] is equal to '' at some point.

The reason for this is most likely that you either have empty fields or an empty line in your *.csv file. Print line and value to see whats going on.

This should help you to figure out the problem in your code and/or data, and how to read Python exceptions. But after you fixed this bug you should really use the built in csv module to parse CSV files, like @Joran Beasley suggested.

from csv import DictReader

print list(DictReader(open("devices.txt")))

seems much easier to me

它正在尝试将以逗号分隔的行上的第0个值转换为整数(使用10为底,但该底可能不相关。)此错误很可能意味着实际位于第0个位置的字符串该行不是可识别的数字(例如,您的devices.txt格式可能不正确,以某行开头为lounge,lamp,False, ...而不是数字,或者id字段有时为空(例如,lounge,lamp,... )。

The error message ValueError: invalid literal for int() with base 10: '' means the following:

You asked Python to convert an object to an integer by calling int().

One way you can fix this is by doing the following: turn value into a string and then use the replace() method like this.

value = value.replace(' ', '') 

Note: You can insert anything into the first set of '' . This will replace it with whatever is in the second set of '' . Be sure to leave the second set without any spaces or characters if you want it to be removed.

Do this for every single character that can not be turned into an integer. These include: !@#$%^&*()_+{}[]":;'?><,./|+=-

To find out which characters you have that might not be convertible to an integer do the following:

print(repr(value))

This will show all of your characters. Just use the replace method on any that you can not convert.

Hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM