-1

I would like to search a csv column and have my script return the total number of times each cell phone is used... Here is my code, but I am not sure what the problem is...

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

I've also included the error I am receiving.

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'
Erica
  • 2,399
  • 5
  • 26
  • 34
John
  • 1
  • 1

1 Answers1

0

unsupported operand type(s) for +: 'int' and 'str'

This indicates that you're (essentially) trying to add a number and letter together. Python sees 0 (an integer) and '0' (a string) as different, and can't add them together. Since you are using iPhone (and Android and other) as counters, you will want to add two integers together.


I'm also not clear on what you're trying to add to that counter, though.

iPhone = iPhone + int(iPhone)

If iPhone = 0, then that formula is iPhone = 0 + 0. If you'd already counted 2 iPhones, that formula is iPhone = 2 + 2 -- you're doubling the value rather than incrementing.

You probably just want:

iPhone = iPhone + 1

or (shorter)

iPhone += 1
Community
  • 1
  • 1
Erica
  • 2,399
  • 5
  • 26
  • 34