我想第一次使用python CSV阅读器.我有一个方法要求用户选择他们想要解析的文件,然后它将该文件路径传递给parse方法:
def parse(filename): parsedFile = [] with open(filename, 'rb') as csvfile: dialect = csv.Sniffer().sniff(csvfile.read(), delimiters=';,|') csvfile.seek(0) reader = csv.reader(csvfile, dialect) for line in reader: parsedFile.append(line) return(parsedFile) def selectFile(): print('start selectFile method') localPath = os.getcwd() + '\Files' print(localPath) for fileA in os.listdir(localPath): print (fileA) test = False while test == False: fileB = input('which file would you like to DeID? \n') conjoinedPath = os.path.join(localPath, fileB) test = os.path.isfile(conjoinedPath) userInput = input('Please enter the number corresponding to which client ' + fileB + ' belongs to. \n\nAcceptable options are: \n1.A \n2.B \n3.C \n4.D \n5.E \n') client = '' if (userInput == '1'): client = 'A' elif (userInput == '2'): client = 'B' elif (userInput == '3'): client = 'CServices' elif (userInput == '4'): client = 'D' elif (userInput == '5'): client = 'E' return(client, conjoinedPath) def main(): x, y = selectFile() parse(y) if __name__ == '__main__': main()
所有这些似乎按预期工作,但我得到一个:
TypeError: can't use a string pattern on a bytes-like object
尝试打开文件名时(代码中的第3行).我试图将文件名转换为字符串类型和字节类型,似乎都不起作用.
这是输出:
>>> start selectFile method C:\PythonScripts\DeID\Files 89308570_201601040630verifyppn.txt 89339985_201601042316verifyppn.txt which file would you like to DeID? 89339985_201601042316verifyppn.txt Please enter the number corresponding to which client 89339985_201601042316verifyppn.txt belongs to. Acceptable options are: 1.Client A 2.Client B 3.Client C 4.Client D 5.Client E 3 Traceback (most recent call last): File "C:\PythonScripts\DeID\DeIDvA1.py", line 107, inmain() File "C:\PythonScripts\DeID\DeIDvA1.py", line 103, in main parse(y) File "C:\PythonScripts\DeID\DeIDvA1.py", line 63, in parse dialect = csv.Sniffer().sniff(csvfile.read(), delimiters=';,|') File "C:\Python34\lib\csv.py", line 183, in sniff self._guess_quote_and_delimiter(sample, delimiters) File "C:\Python34\lib\csv.py", line 224, in _guess_quote_and_delimiter matches = regexp.findall(data) TypeError: can't use a string pattern on a bytes-like object >>>
我不确定我做错了什么.
这里不是要归咎的文件名,而是你打开文件的事实:
with open(filename, 'rb') as csvfile:
当'rb'
模式指定的文件将在二进制模式打开,也就是说,该文件的内容被视为byte
对象.文件:
'b'
附加到模式后以二进制模式打开文件:现在以bytes对象的形式读取和写入数据.此模式应该用于所有不包含文本的文件.
然后,您尝试csv.Sniff().sniff()
使用字符串模式在其中进行搜索,并且正如TypeError
优雅地指出的那样,这是不允许的.
b
从模式中删除并简单地使用r
将可以解决问题.
注意:Python 2.x在Unix机器上不会出现此行为.这是将对象bytes
和str
对象分离为不同类型的结果3.x
.