导致此语法错误的原因是什么?

时间:2013-07-03 17:07:17

标签: python

感觉自由发送这个帖子SINEC它是由一个TYPO引起的...... APOLOGIES

我无法弄清楚为什么这个try和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')

如果有帮助,我已在示例电子表格中提供了HERE链接。

非常感谢!

2 个答案:

答案 0 :(得分:4)

此处缺少]

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

它应该是:

mysheet_list[rowvalue+1]['email']

答案 1 :(得分:2)

row['alt_email'] = mysheet_list[rowvalue+1['email']中您错过了]

修正:

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