UnicodeDecodeError:'utf-8'编解码器无法解码位置33中的字节0xb1:读取Matlab文件时无效的起始字节

时间:2017-01-12 18:25:22

标签: python matlab utf-8

我试图找到一个缺少的Matlab脚本,我需要进行一些数据处理。我写了一些python代码来读取我正在处理的计算机系统上的所有matlab文件,因为这里有成千上万的matlab脚本和我使用python比使用matlab更舒服。当我运行我写的代码来找到字符串'11 -rxnCMP'时,我必须在我正在寻找的脚本中存在,我不断收到此错误。代码如下:

import os
import codecs 

address = 'X:\Data'
fileNames = []   #contains the full file addresses
OneName = []    #contains individual fileNames
fileCorrect = []
#list all folders to iterate through them later on
for subdir, dirs, files in os.walk(address):
    for file in os.listdir(subdir):
        if file.endswith(".m"):
            print(subdir,file)
            fileName = file            
            OneName.append(fileName)
            fileAddress = subdir+'/'+ file
            if 'Retired' in fileAddress:
                continue
            else:
                fileNames.append(fileAddress)
#open and read the files that are stored in fileNames

if len(fileNames) != 0:
    for i in range(len(fileNames)):
        lineNum = 0 
        with codecs.open(fileNames[i],mode = 'r', encoding = 'utf8') as fileOpen:
            for line in iter(fileOpen):
                if 'Retired' in fileOpen.name:                
                    break
                else:            
                    lineNum += 1
                    try:
                        if 'rxnCMP' in line:
                            fileCorrect.append(fileNames[i])
                            print(line)
                            print('Script' + fileNames[i] + ' has this word in it ')
                            print(lineNum)
                            continue 
                        else: 
                            continue 
                    except:
                        break

我得到的错误是:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb1 in position 33: invalid start byte

换行:

for line in iter(fileOpen)

我不确定为什么会这样。可能是我写的matlab脚本编码方式不同。

0 个答案:

没有答案