我正在尝试编写一个代码,将每个状态的所有“计数”加起来。这是我的.dbf(csv)文件的示例。
Count Lat Lon index_righ STATE
1345 31.551580000000000 -88.448380000000000 1992 Alabama
445 31.999867999999900 -88.404543000000000 1992 Alabama
299 32.448521000000000 -88.360196000000000 1992 Alabama
106 32.897511999999900 -88.315329000000000 1992 Alabama
229 33.346817000000000 -88.269934000000000 1992 Alabama
270 33.796408999999900 -88.224000000000000 1992 Alabama
217 34.246262999999900 -88.177518000000000 1992 Alabama
我希望输出看起来像这样:
Count State
2911 Alabama
etc. etc.
这是我的代码。
result = {}
with open('C:/Users/brownk98/Desktop/result_out.dbf', 'rt') as f:
count = 0
#r = csv.DictReader(csvfile, delimiter=',')
for row in f:
values = row.split(",")
count = None
state = None
count = int(values[0])
state = str(values[4])
if state is not None:
if state not in result :
result[state] += count
else:
result[state] = count
print(result)
我收到错误:ValueError:以10为底的int()的无效文字:'\ x03u \ x07 \ n'
任何帮助,将不胜感激。
最佳答案
@kayla,你们都是正确的。不确定为什么会引发错误。这是我尝试过的,并且有效。请注意,我已经用您的输入准备了一个“ .csv”文件
result= {}
with open('trail.csv') as f:
reader = csv.reader(f)
next(reader)
for row in reader:
count, state = int(row[0]), row[4]
if state in result.keys():
result[state] += count
else:
result[state] = count
print( result )
>> {'Alabama': 2911}
考虑到您的情况,如果文件很大并且无法将其转换为.CSV,请尝试使用制表符(即
values = row.split("\t")
)进行拆分关于python - 根据另一列中的str在一列中添加int值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45013957/