goldisfine
goldisfine

Reputation: 4850

What's causing this syntax error?

FEEL FREE TO FLAG THIS POST SINEC IT WAS CAUSED BY A TYPO....APOLOGIES

I cannot figure out for the life of me why this try and except statement is return a syntax error. Is it something earlier in the code / a typo?

import csv

# This function takes a tab-delim csv and merges the ones with the same name but different phone / email / websites.
def merge_duplicates(sheet):

    with(open(sheet, 'rU')) as f:

        mysheet = csv.DictReader(f, delimiter = '\t')    
        mysheet_list = list(mysheet)

        for rowvalue, row in enumerate(mysheet_list):
            print rowvalue, row

            try:
                if row['name'] == mysheet_list[rowvalue+1]['name']:
                    if row['email'] != mysheet_list[rowvalue+1]['email']:
                        row['alt_email'] = mysheet_list[rowvalue+1['email']
#                     if row['website'] != mysheet_list[rowvalue+1]['website']:
#                         row['alt_website'] != mysheet_list[rowvalue+1]['website']
            except IndexError:
                print("We're at the end now") 

merge_duplicates('ieca_first_col_fake_text.txt')

If it helps, I've provided a link, HERE to the example spreadsheet.

Thanks so much!

Upvotes: 1

Views: 85

Answers (2)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251186

Missing ] here:

mysheet_list[rowvalue+1['email']
                       ^
                       |

it should be:

mysheet_list[rowvalue+1]['email']

Upvotes: 4

IT Ninja
IT Ninja

Reputation: 6438

In row['alt_email'] = mysheet_list[rowvalue+1['email'] You are missing a ].

Fixed:

row['alt_email'] = mysheet_list[rowvalue+1['email']]

Upvotes: 2

Related Questions