简体   繁体   English

从文件读取时发生ValueError

[英]ValueError when reading from a file

I have a python script that reads items from a file, devices.txt and then populates a dictionary. 我有一个python脚本,该脚本从文件devices.txt中读取项目,然后填充字典。 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. 现在,当python单独读取时,就可以了。 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? 考虑到这一点,我看不到在哪里找到以10为底的int(),我在数学上很糟糕,但是据我了解,因为它以10为底,所以它会看到一个小数正确?

The error message ValueError: invalid literal for int() with base 10: '' means the following: 错误消息ValueError: invalid literal for int() with base 10: ''表示以下含义:

You asked Python to convert an object to an integer by calling int() . 您要求Python通过调用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) ). 因为您没有指定基数,所以它假定默认值为10 (十进制)(例如,您可以通过指定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). 因此,不能使用以10为底的空字符串(在这种情况下为任何其他基)将空字符串''转换为整数。

The exact line of code where this call to int() happened is printed just above the exception: 在该异常的正上方显示了对int()调用发生的确切代码行:

unId = int(value[0])

So you know that value[0] is equal to '' at some point. 因此,您知道在某些时候value[0]等于''

The reason for this is most likely that you either have empty fields or an empty line in your *.csv file. 造成这种情况的原因很可能是您的*.csv文件中包含空白字段或空白行。 Print line and value to see whats going on. 打印linevalue以查看发生了什么。

This should help you to figure out the problem in your code and/or data, and how to read Python exceptions. 这应该可以帮助您找出代码和/或数据中的问题,以及如何读取Python异常。 But after you fixed this bug you should really use the built in csv module to parse CSV files, like @Joran Beasley suggested. 但是,修复此错误之后,您应该真正使用内置的csv模块来解析CSV文件,如@Joran Beasley建议的那样。

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: 错误消息ValueError:int()以10为底的无效文字:``''表示以下含义:

You asked Python to convert an object to an integer by calling int(). 您要求Python通过调用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转换为字符串,然后使用如下的replace()方法。

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. 只需对任何无法转换的地方使用replace方法。

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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