1

随意标记此帖子 SINEC 它是由错字引起的......道歉

我一生都无法弄清楚为什么这个 try and except 语句会返回语法错误。它是代码中较早的内容/错字吗?

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')

如果有帮助,我提供了一个链接,这里是示例电子表格。

非常感谢!

4

2 回答 2

4

这里缺少]

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

它应该是:

mysheet_list[rowvalue+1]['email']
于 2013-07-03T17:09:34.847 回答
2

row['alt_email'] = mysheet_list[rowvalue+1['email']您缺少一个].

固定的:

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

于 2013-07-03T17:09:22.660 回答