计算csv列中发生事件的次数
我想搜索一个csv列,让我的脚本返回每个手机的总使用次数。。。这是我的代码,但我不确定问题出在哪里计算csv列中发生事件的次数,csv,for-loop,arcpy,Csv,For Loop,Arcpy,我想搜索一个csv列,让我的脚本返回每个手机的总使用次数。。。这是我的代码,但我不确定问题出在哪里 import arcpy fc = "C:\Script\SAMPLES\SAMPLES.csv" field = "phone" iPhone = 0 Android = 0 other = 0 cursor = arcpy.SearchCursor(fc) for row in cursor: #print(row.getValue(field)) if row.getValu
import arcpy
fc = "C:\Script\SAMPLES\SAMPLES.csv"
field = "phone"
iPhone = 0
Android = 0
other = 0
cursor = arcpy.SearchCursor(fc)
for row in cursor:
#print(row.getValue(field))
if row.getValue(field)=='iPhone':
iPhone = iPhone + str(iPhone)
print "The number of iPhones: " + iPhone
elif:
Android=Android + str(Android)
print "The number of Androids: " + Android
elif:
other=other + str(other)
print "The number of other: " + other
我还包括了我收到的错误
Traceback (most recent call last):
File "C:\Python27\ArcGIS10.4\Lib\site- packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
exec codeObject in __main__.__dict__
File "C:\Script\searchcursor.py", line 11, in <module>
iPhone = iPhone + str(iPhone)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
回溯(最近一次呼叫最后一次):
RunScript中的文件“C:\Python27\ArcGIS10.4\Lib\site-packages\pythonwin\framework\scriptutils.py”,第326行
主目录中的exec codeObject__
文件“C:\Script\searchcursor.py”,第11行,在
iPhone=iPhone+str(iPhone)
TypeError:不支持+:“int”和“str”的操作数类型
+:“int”和“str”的操作数类型不受支持
这表明您(本质上)正在尝试将数字和字母相加。Python认为0
(一个整数)和'0'
(一个字符串)是不同的,不能将它们相加。由于您使用的是iPhone
(和Android
和other
)作为计数器,因此需要将两个整数相加
不过,我也不清楚你想在计数器上添加什么
iPhone = iPhone + int(iPhone)
如果iPhone=0
,那么这个公式就是iPhone=0+0
。如果您已经计算了2部iPhone,那么公式是iPhone=2+2
——您将值加倍,而不是递增
你可能只是想:
iPhone = iPhone + 1
或()
iPhone += 1