SO im working on a python code that reads a csv file with 1600 entries roughly and i am splitting them up on the collum value of pc numbers. I keep getting a error that states
"

Traceback (most recent call last):
  File "C:\Users\HatterX\Desktop\test\test25", line 50, in <module>
    data.sort(key=lambda x: x[2])
  File "C:\Users\HatterX\Desktop\test\test25", line 50, in <lambda>
    data.sort(key=lambda x: x[2])
IndexError: list index out of range

".
Could someone tell me what im doing wrong please this has me very conffused and frustrated.

import csv, datetime, os, shutil, glob

newrow={'PC Number': '', 'INSTALL NEW LUXURY SIGN - YES/NO': '',  'Status': '',  'Street Address': '',  'City': '', 'State': '',  'Zip': '',  'County': '', 'MLS#': '',  'Price': '',  'Agent Name': '',  'Office Name': '', 'Region': '',  'Office Address 1': '',  'Office Address 2': '',  'Office City': '',  'Office State': '',  'Office Zip': '',  'Office Phone': '',  'Number Manager': '',  'Name Manager': '',  'Email Address': '',  'Admin Name': '',  'Admin Email Address': '', 'Listing Expiration Date': ''}
new_field_names = newrow.keys()
dt = datetime.datetime.now().strftime("%m_%d_%y_%H_%M_%S")

os.chdir("/Users/HatterX/Desktop/Unprocessed Trello")
for FILE in glob.glob("LuxuryListings-040714*"):
    with open(FILE, 'r') as f1, open('/Users/HatterX/Desktop/Trello Update/Trello_add_'+dt+'.csv', 'ab') as f2:
        cf1 = csv.DictReader(f1, fieldnames=('PC Number', 'INSTALL NEW LUXURY SIGN - YES/NO',  'Status',  'Street Address',  'City',  'State',  'Zip',  'County',  'MLS#',  'Price',  'Agent Name',  'Office Name',  'Region',  'Office Address 1',  'Office Address 2',  'Office City',  'Office State',  'Office Zip',  'Office Phone',  'Number Manager',  'Name Manager',  'Email Address',  'Admin Name',  'Admin Email Address', 'Listing Expiration Date' ))
        cf2 = csv.DictWriter(f2, new_field_names)
        cf2.writeheader()
        for row in cf1:
            nr = newrow
            nr['PC Number'] = row['PC Number'].strip()
            nr['INSTALL NEW LUXURY SIGN - YES/NO'] = row['INSTALL NEW LUXURY SIGN - YES/NO'].strip()
            nr['Status'] = row['Status'].strip()
            nr['Street Address'] = row['Street Address'].strip()
            nr['City'] = row['City'].strip()
            nr['State'] = row['State'].strip()
            nr['Zip'] = row['Zip'].strip()
            nr['County'] = row['County'].strip()
            nr['MLS#'] = row['MLS#'].strip()
            nr['Price'] = row['Price'].strip()
            nr['Agent Name'] = row['Agent Name'].strip()
            nr['Office Name'] = row['Office Name'].strip()
            nr['Region'] = row['Region'].strip()
            nr['Office Address 1'] = row['Office Address 1'].strip()
            nr['Office Address 2'] = row['Office Address 2'].strip()
            nr['Office City'] = row['Office City'].strip()
            nr['Office State'] = row['Office State'].strip()
            nr['Office Zip'] = row['Office Zip'].strip()
            nr['Office Phone'] = row['Office Phone'].strip()
            nr['Number Manager'] = row['Number Manager'].strip()
            nr['Name Manager'] = row['Name Manager'].strip()
            nr['Email Address'] = row['Email Address'].strip()
            nr['Admin Name'] = row['Admin Name'].strip()
            nr['Admin Email Address'] = row['Admin Email Address'].strip()
            nr['Listing Expiration Date'] = row['Listing Expiration Date']
            print nr
            cf2.writerow(nr)

data=[]
os.chdir("/Users/Hatterx/Desktop/Trello Update")
for file in glob.glob("Trello_add*"):          
    with open(file) as fid:   
        lines=fid.read().split('\n')
for i in lines: 
    data.append(i.split(','))
data.sort(key=lambda x: x[2])
for d in data:
    field=d[2]
    with open('pc_Numbers_'+field+'.csv') as fid:
        while d[2]==field: 
            fid.write(d+'\n')
IndexError: list index out of range

means that at least one of the items in "x" has less than 3 elements. Before the sort, run some code to print anything with less than 3 elements and decide what you want to do with those records.

Note also that this while loop will never exit because d[2] and field do not change, (and you can not write to read only file).

    while d[2]==field: 
        fid.write(d+'\n')

thank you woooee i actualyl just solved this issue now im working on trying to do a if file exist then append but if it doesnt write the header row

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.