【发布时间】:2020-02-17 23:08:42
【问题描述】:
我正在尝试将 CSV 文件导入 Python。导入 CSV 后,我想获取每个 ['Spent Past 6 Months'] 值,但是 CSV 包含在该值前面的“$”符号给我带来了问题。我已经尝试了很多方法来摆脱那个符号,但我真的迷失了这一点!
我对 Python 真的很陌生,所以如果这里有一些非常简单的东西我遗漏了,我深表歉意。
下面列出了我编码的内容。我的输出首先列出:
File "customer_regex2.py", line 24, in <module>
top20Cust = top20P(data)
File "customer_regex2.py", line 15, in top20P
data1 += data1 + int(a[i]['Spent Past 6 Months'])
ValueError: invalid literal for int() with base 10: '$2099.83'
import csv
import re
data = []
with open('customerData.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
data.append(row)
def top20P(a):
outputList=[]
data1=0
for i in range(0,len(a)):
data1 += data1 + int(a[i]['Spent Past 6 Months'])
top20val= int(data1*0.8)
for j in range(0,len(a)):
if data[j]['Spent Past 6 Months'] >= top20val:
outputList.append('a[j]')
return outputList
top20Cust = top20P(data)
print(outputList)
【问题讨论】:
-
这里有一个类似的问题,你可以在这里找到解决方案:stackoverflow.com/questions/17176542/…
标签: python python-3.x csv