2013-12-20 13 views
4

我尝试使用Python和xlrd打开一个Excel工作表,但我还是有以下错误的Python 2.7类型错误:文件()参数1必须编码字符串没有NULL字节,而不是str的

error in open_workbook:

f = open(filename, "rb")

TypeError: file() argument 1 must be encoded string without NULL bytes, not str.

这是我的代码:

FILE = tkFileDialog.askopenfile() 
string=FILE.read() 
wb = xlrd.open_workbook(string) 

请问这是怎么回事?非常感谢

+0

'print repr(filename)'并告诉我们你得到了什么。 – user2357112

+0

看起来您正在阅读的文件中有空字节以获取文件名。 – user2357112

+0

print repr(FILE)你的意思是?我得到:<打开文件u'C:/Users/pmarguer/Documents/PM/FSM1_GSM_List_Parameters_V3.01_AI.xlsm',模式'r'在0x02A46C28> –

回答

0

您需要使用askopenfilename而不是askopenfile,除非您确实在传递文件而不是名称。

FILE = tkFileDialog.askopenfilename() 
string = FILE.read() 
wb = xlrd.open_workbook(string) 
相关问题