Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: list indices must be integers or slices, not str

I've got two lists that I want to merge into a single array and finally put it in a csv file. I'm a newbie with Python arrays and I don't understand how I can avoid this error :

def fill_csv(self, array_urls, array_dates, csv_file_path):     result_array = []     array_length = str(len(array_dates))      # We fill the CSV file     file = open(csv_file_path, "w")     csv_file = csv.writer(file, delimiter=';', lineterminator='\n')      # We merge the two arrays in one      for i in array_length:         result_array[i][0].append(array_urls[i])         result_array[i][1].append(array_dates[i])         i += 1      csv_file.writerows(result_array) 

And got :

  File "C:\Users\--\gcscan.py", line 63, in fill_csv     result_array[i][0].append(array_urls[i]) TypeError: list indices must be integers or slices, not str 

How can my count work ?

like image 683
Arthur C-G Avatar asked Sep 13 '15 21:09

Arthur C-G


People also ask

How do you fix list indices must be integers or slices not dict?

The Python "TypeError: list indices must be integers or slices, not dict" occurs when we use a dictionary to access a list at a specific index. To solve the error, use an integer or a slice for list indexes, e.g. my_list[0] .

What does list indices must be integers or slices not list mean in Python?

The Python "TypeError: list indices must be integers or slices, not float" occurs when we use a floating-point number to access a list at a specific index. To solve the error, convert the float to an integer, e.g. my_list[int(my_float)] . Here is an example of how the error occurs. Copied!

How resolve TypeError string indices must be integers?

You cannot access a value in a string using another string. To solve TypeError: string indices must be integers; we should reference our dictionary instead of “ele“. We can access elements in our dictionary using a string. This is because dictionary keys can be strings.

How do I fix TypeError list indices must be integers or slices not tuple?

The Python "TypeError: list indices must be integers or slices, not tuple" occurs when we pass a tuple between the square brackets when accessing a list at index. To solve the error, make sure to separate nested list elements with commas and correct the index accessor.


2 Answers

First, array_length should be an integer and not a string:

array_length = len(array_dates) 

Second, your for loop should be constructed using range:

for i in range(array_length):  # Use `xrange` for python 2. 

Third, i will increment automatically, so delete the following line:

i += 1 

Note, one could also just zip the two lists given that they have the same length:

import csv  dates = ['2020-01-01', '2020-01-02', '2020-01-03'] urls = ['www.abc.com', 'www.cnn.com', 'www.nbc.com']  csv_file_patch = '/path/to/filename.csv'  with open(csv_file_patch, 'w') as fout:     csv_file = csv.writer(fout, delimiter=';', lineterminator='\n')     result_array = zip(dates, urls)     csv_file.writerows(result_array) 
like image 83
Alexander Avatar answered Oct 01 '22 22:10

Alexander


Follow up on Abdeali Chandanwala answer above (couldn't comment because rep<50) -

TL;DR: I was trying to iterate through a list of dictionaries incorrectly by focusing to iterate over the keys in the dictionary but instead had to iterate over the dictionaries themselves!


I came across the same error while having a structure like this:

{    "Data":[       {          "RoomCode":"10",          "Name":"Rohit",          "Email":"[email protected]"       },       {          "RoomCode":"20"          "Name":"Karan",          "Email":"[email protected]"       }    ] } 

And I was trying to append the names in a list like this-

Same error received

Fixed it by-

Fixed the error

like image 21
rohetoric Avatar answered Oct 01 '22 23:10

rohetoric