Ankush Soni
Ankush Soni

Reputation: 55

Python3 TypeError: string indices must be integers

I am new to python programming. I'm trying to implement a code which reads data from a file and displays it in kind of a tabular format. However, when I try to run my code, it gives the error as:

TypeError: string indices must be integers

Here is my code:

from operator import itemgetter
emp_dict = dict()
emp_list = list()
with open('m04_lab_profiles','r') as people:
    for p in people:
        emp_list = p.strip().split(',')
        emp_info = dict()
        emp_info['Name'] = emp_list[0]
        emp_info['Location'] = emp_list[1]
        emp_info['Status'] = emp_list[2]
        emp_info['Employer'] = emp_list[3]
        emp_info['Job'] = emp_list[4]
        emp_dict[emp_list[0]] = emp_list
        emp_list.append(emp_info)
for info in emp_list:
         print("{0:20}   {1:25}  {2:20}  {3:20}  {4:45}".format(int(info['Name'],info['Location'],info['Status'],info['Employer'],info['Job'])))

print("\n\n")
info_sorted = sorted(emp_list,key=itemgetter('Name'))
for x in info_sorted:
     print("{0:20}   {1:25}  {2:20}  {3:20}  
     {4:45}".format(emp_info['Name'],

     emp_info['Address'],                                       
     emp_info['Status'],                                                       
     emp_info['Employer'],                                                       
     emp_info['Job']))

I've tried almost every other solution given for the same question title, but all went in vain. Please help

Upvotes: 1

Views: 861

Answers (1)

Andrew F
Andrew F

Reputation: 2950

The issue is that you're using emp_list inside of your loop as well as outside. The result is that your list once you've loaded the file has some elements that are strings (which require an integer index) and some elements that are dicts (which have more flexible indexing rules). Specifically, with an example file that looks like

name,location,status,employer,job
name2,location2,status2,employer2,job2

After the loop, the emp_list looks like

In [3]: emp_list
Out[3]:
['name2',
 'location2',
 'status2',
 'employer2',
 'job2',
 {'Name': 'name2',
  'Location': 'location2',
  'Status': 'status2',
  'Employer': 'employer2',
  'Job': 'job2'}]

The fix to this is to use a different temporary list as the output of your .split(',') call. I.e.

In [4]: from operator import itemgetter
   ...: emp_dict = dict()
   ...: emp_list = list()
   ...: with open('m04_lab_profiles','r') as people:
   ...:     for p in people:
   ...:         tmp = p.strip().split(',')
   ...:         emp_info = dict()
   ...:         emp_info['Name'] = tmp[0]
   ...:         emp_info['Location'] = tmp[1]
   ...:         emp_info['Status'] = tmp[2]
   ...:         emp_info['Employer'] = tmp[3]
   ...:         emp_info['Job'] = tmp[4]
   ...:         emp_dict[tmp[0]] = emp_info
   ...:         emp_list.append(emp_info)
   ...:
   ...:

In [5]: emp_list
Out[5]:
[{'Name': 'name',
  'Location': 'location',
  'Status': 'status',
  'Employer': 'employer',
  'Job': 'job'},
 {'Name': 'name2',
  'Location': 'location2',
  'Status': 'status2',
  'Employer': 'employer2',
  'Job': 'job2'}]

Upvotes: 2

Related Questions