[英]How do I convert dictionary list values to integers?
I'm working in Python 3.5. 我正在使用Python 3.5。 I've uploaded a CSV file and made it into a dictionary.
我上传了一个CSV文件,并将其制成字典。 However, the list of multiple values for each key is a string and not an integer.
但是,每个键的多个值列表是一个字符串,而不是整数。 How can I convert the values for each key into an integer?
如何将每个键的值转换为整数?
Furthermore, is there a way for future CSV importation to automatically make dictionary value lists into integers? 此外,是否存在将来CSV导入的方法来自动将字典值列表转换为整数?
So far this is what I have: 到目前为止,这就是我所拥有的:
import csv
reader = csv.reader(open('filename.csv'))
dictname = {}
for row in reader:
key = row[0]
if key in dictname:
pass
dictname[key] = row[1:]
print dictname
if row is the list containing the integers under string format : 如果row是包含以字符串格式表示的整数的列表:
dictname[key] = [int(elt) for elt in row[1:] if elt.isdigit()]
should do the trick 应该可以
You can use pandas and specify converter functions. 您可以使用熊猫并指定转换器功能。 In fact you may not even need to do that as it is intelligently parsing CSV files.
实际上,您甚至不需要这样做,因为它可以智能地解析CSV文件。
import pandas as pd
df = pd.read_csv('filename.csv')
If you need converter functions: 如果需要转换器功能:
df = pd.read_csv('filename.csv',converters={'yourintegercolumn':int})
I used a function to first check if the value is a string/unicode. 我使用一个函数来首先检查该值是否为字符串/ Unicode。 If so, it then tries to convert it to a float, eg "1,234.45" -> 1234.45.
如果是这样,它将尝试将其转换为浮点数,例如“ 1,234.45”-> 1234.45。 If this fails or the value is not a string/float, the function returns it unchanged.
如果失败或该值不是字符串/浮点型,则该函数将其保持不变。
This function is then used in a list comprehension to population the dictionary. 然后在列表推导中使用此函数填充字典。
Note that the if key in dictname: pass
block doesn't do anything. 请注意,
if key in dictname: pass
块中的if key in dictname: pass
不会执行任何操作。 If there are duplicate keys in your data, you have three options: 如果您的数据中有重复的键,则有以下三种选择:
1) Overwrite the existing key's data with the new row that has the identical key value (this is what is currently happening). 1)用具有相同键值的新行覆盖现有键的数据(这是当前正在发生的情况)。
2) Only use the first occurrence of the key row. 2)仅使用键行的第一个匹配项。 In this case, change
pass
to continue
. 在这种情况下,请更改
pass
continue
。
3) Try to aggregate the data. 3)尝试汇总数据。 This is more complicated and beyond the scope of your original question, so I will leave it to you to figure out or post a new question covering this scope.
这更加复杂,超出了您原始问题的范围,因此我将留给您找出或发布涵盖此问题的新问题。
def convert_to_numeric(value):
if isinstance(i, (str, unicode)):
try:
result = float(value)
except:
pass # Returns result on next line.
return result
for row in reader:
key = row[0]
if key in dictname:
pass # This doesn't do anything. Use `continue` to avoid overwriting.
dictname[key] = [convert_to_numeric(i) for i in row[1:]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.