Python 将字典值转换为整数

Python 将字典值转换为整数,python,Python,我在将一些数字从字符串转换为整数时遇到问题。以下是所讨论的功能: def read_discounts(): myFile = open('discount.txt', 'r') discountValues = {} #read and split first line firstLine = myFile.readline() firstLine = re.sub(r'\$','',firstLine) firstLine = re.sub(r'\%','',fir

我在将一些数字从字符串转换为整数时遇到问题。以下是所讨论的功能:

def read_discounts():
  myFile = open('discount.txt', 'r')
  discountValues = {}

  #read and split first line
  firstLine = myFile.readline()
  firstLine = re.sub(r'\$','',firstLine)
  firstLine = re.sub(r'\%','',firstLine)
  firstLine = firstLine.split()

  #add values to dictionary
  discountValues['UpperLimit1'] = {firstLine[2]}
  int(discountValues['UpperLimit1'])
  discountValues['PercentDiscount1'] = {firstLine[4]}
和回溯:

Traceback (most recent call last):
File "C:\Users\Sam\Desktop\test.py", line 94, in <module>
main()
File "C:\Users\Sam\Desktop\test.py", line 6, in main
discounts = read_discounts()
File "C:\Users\Sam\Desktop\test.py", line 33, in read_discounts
int(discountValues['UpperLimit1'])
TypeError: int() argument must be a string or a number, not 'set'
回溯(最近一次呼叫最后一次):
文件“C:\Users\Sam\Desktop\test.py”,第94行,在
main()
文件“C:\Users\Sam\Desktop\test.py”,第6行,主目录
折扣=阅读折扣()
文件“C:\Users\Sam\Desktop\test.py”,第33行,以只读形式
int(折扣值['UpperLimit1'])
TypeError:int()参数必须是字符串或数字,而不是“set”
我有点不知所措,但是我知道
discountValues['UpperLimit']
是一个应该能够转换为整数的值(
100

我尝试的内容:我尝试将字符串列表中的值在添加到字典之前进行转换,得到了相同的结果。我也尝试过使用dict理解,但这似乎会在以后使用该值时引起问题


任何建议都将不胜感激,谢谢。

您以错误的方式指定了词汇值。应该是

discountValues['UpperLimit1'] = firstLine[2] # Droped the { and } from assignment
int(discountValues['UpperLimit1'])
discountValues['PercentDiscount1'] = firstLine[4]
{}
中包装东西将创建

测试

>>> a_dict = {}
>>> a_dict["set"] = {"1"} # creates a set and assign it to a_dict["set"]
>>> type(a_dict["set"])
<class 'set'>
>>> a_dict["string"] = "1" # Here a string value is assigned to a_dict["string"]

>>> type(a_dict["string"])
<class 'str'>

>>> int(a_dict["string"])
1
>>> int(a_dict["set"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a number, not 'set'

您正在以错误的方式分配词汇值。应该是

discountValues['UpperLimit1'] = firstLine[2] # Droped the { and } from assignment
int(discountValues['UpperLimit1'])
discountValues['PercentDiscount1'] = firstLine[4]
{}
中包装东西将创建

测试

>>> a_dict = {}
>>> a_dict["set"] = {"1"} # creates a set and assign it to a_dict["set"]
>>> type(a_dict["set"])
<class 'set'>
>>> a_dict["string"] = "1" # Here a string value is assigned to a_dict["string"]

>>> type(a_dict["string"])
<class 'str'>

>>> int(a_dict["string"])
1
>>> int(a_dict["set"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a number, not 'set'

因为您在
第一行[2]
周围添加了
{}
,所以它是一个集合。如果删除
{}
s,它应该可以工作


此外,正如上面的一条注释所述,在将值转换为int之后,实际上需要保存该值。仅调用
int(discountValues['UpperLimit1'])
实际上不会保存该数字。如果你想让你的字典里有整数而不是字符串,可以试试像
discountValues['UpperLimit1']=int(firstLine[2])
这样的方法。

因为你在
firstLine[2]
周围添加了
{}
,它就成了一个集合。如果删除
{}
s,它应该可以工作


此外,正如上面的一条注释所述,在将值转换为int之后,实际上需要保存该值。仅调用
int(discountValues['UpperLimit1'])
实际上不会保存该数字。如果你想让你的字典里有整数而不是字符串,可以试试像
discountValues['UpperLimit1']=int(firstLine[2])
这样的方法。

去掉第一行[]周围的{和}一旦你把错误整理好,可能值得注意的是
int(discountValues['UpperLimit1'])
没有任何作用
int(somevalue)
不会将
somevalue
就地转换为int;为此,您需要
somevalue=int(somevalue)
。谢谢。特别是Kevin,这真的救了我。去掉第一行[]周围的{和}一旦你解决了错误,可能值得注意的是
int(discountValues['UpperLimit1'])
没有任何作用
int(somevalue)
不会将
somevalue
就地转换为int;为此,您需要
somevalue=int(somevalue)
。谢谢。尤其是凯文,这真的救了我。如果投票人能指出答案的错误,那将非常有帮助。谢谢你的回复。我纠正了字典值的赋值。现在没有错误,但字典值似乎仍然是字符串。你知道我做错了什么吗?因为你只说了
int(折扣值['UpperLimit1'])
而不是
discountValues['UpperLimit1']=int(折扣值['UpperLimit1'])
int()
调用返回一个值-它不会就地修改传递的对象。@c3066521请查看编辑。我想这就是你要找的,别介意我把它分类了,正如上面kevin所指出的,我使用int()时不正确。再次感谢。如果投票人能指出答案的错误,那将非常有帮助。谢谢你的回复。我纠正了字典值的赋值。现在没有错误,但字典值似乎仍然是字符串。你知道我做错了什么吗?因为你只说了
int(折扣值['UpperLimit1'])
而不是
discountValues['UpperLimit1']=int(折扣值['UpperLimit1'])
int()
调用返回一个值-它不会就地修改传递的对象。@c3066521请查看编辑。我想这就是你要找的,别介意我把它分类了,正如上面kevin所指出的,我使用int()时不正确。再次感谢。