程序意外终止

时间:2014-06-25 12:53:24

标签: python file-io exception-handling

我有一个巨大的(1.5 GB)tsv(制表符分隔值)文件,我正在使用python处理,该文件是基于行的,但它有一些格式错误的行,我希望跳过,我的代码是如下:

fo = open(output, 'w')    
with open(filename) as f:
    i = 0
    for line in f:
        print i
        try:  #to account for the ill-formatted lines
            user_hash, artist_hash, artist, playcount = line.split('\t')
            fo.write('{0}\t{1}\t{2}'.format(hash_map[user_hash], artist, playcount))
            i = i+1
        except:
            print "error in user_hash : " + user_hash
            continue

现在的问题是程序在捕获到第一个异常后立即终止执行,它只是打印“user_hash中的错误”然后存在。它应该继续,因为我知道该文件有1700万+行, i 只达到433919。

为什么会这样?

感谢阅读。

1 个答案:

答案 0 :(得分:0)

我想我找到了导致问题的原因,我正在阅读的文件有很多'^ Z'字符,我认为是什么导致程序终止。

那么检测包含这些字符的行并在处理文件时忽略这些行的最佳方法是什么?

相关问题