2016-08-17 43 views
0

我喜欢用csv阅读器读取ASCII文件列表(utf-8)。 对于错误处理,我喜欢检测用户是否因意外选择了无法读取的文件。 来源是这样的:用csv阅读器检测错误的文件格式

for File in Filenames: 
    print ('... processing file :',File) 
    with open(File, 'r') as csvfile: 
     Reader = csv.reader(csvfile, delimiter = ';')   
     for Line in Reader: 
      print(Line) 

我用户选择例如其GZIPed我得到了消息文件:

(结果,消耗)= self._buffer_decode(数据,self.errors,最终) UnicodeDecodeError错误: 'UTF-8' 编解码器不能在位置1解码字节0x8b:无效的起始字节

哪一个可以,但脚本崩溃。 我没有找到如何捕捉错误并强制脚本跳转到列表中的下一个文件。我发现了很多关于方言和其他编解码器的内容,但我的任务是不通过更改编解码器来阅读错误的文件。

非常感谢您的任何评论!

回答

1

如何:

for File in Filenames: 
    print ('... processing file :',File) 
    with open(File, 'r') as csvfile: 
     try: 
      Reader = csv.reader(csvfile, delimiter = ';')   
      for Line in Reader: 
       print(Line) 
     except UnicodeDecodeError as e: 
      print("File {:} cannot be read. Skipping...".format(csvfile)) 
      continue 
0

使用异常处理 - https://docs.python.org/3/tutorial/errors.html

那么你的代码是这样:

for File in Filenames: 
    print ('... processing file :',File) 
    try: 
     with open(File, 'r', encoding='utf-8') as csvfile: 
      Reader = csv.reader(csvfile, delimiter = ';')   
      for Line in Reader: 
       print(Line) 
    except UnicodeDecodeError: 
     pass 

这是很好的做法,包括当你打开你所期望的编码文件。如果您将相同的脚本放在Windows机器上,则默认编码不会是“utf-8”。

+0

你真的不得不在'try'中包装'with'吗?它可以通过一个错误吗? –

+0

IIRC,它是引发UnicodeDecodeError的open()的TextWrapper。 –

+0

非常感谢! –