Python returns the error "TypeError: string indices must be integers, not str"

时间:2017-06-04 23:38:22

标签: python csv

I got this error message when trying to loop over the CSV file.

Error message

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-18-5024001dfc7f> in <module>()
      2 
      3 for result in data:
----> 4     result['Name'] = result['Name']
      5     doc_complete.append(result['Name'])
      6 

TypeError: string indices must be integers, not str

Source code

data = pd.read_csv('data.csv')
for result in data:
    result['Name'] = result['Name']
    doc_complete.append(result['Name'])

1 个答案:

答案 0 :(得分:1)

You cant append to a Python array with result["Name"], you have a string inside the braces when it should be an integer used as an index for whatever you are trying to access.

As for result["Name"] = result["Name"], I have no clue what you are trying to achieve.