错误:
/Users/stern/miniconda3/envs/env_rdkit/bin/python /Users/stern/PycharmProjects/Stern/DTI/EssayLargeScale/generateVocabDict.py
Enzyme_Drug_Structure.xlsx
.~Enzyme_Drug_Structure.xlsx
Traceback (most recent call last):
File "/Users/stern/PycharmProjects/Stern/DTI/EssayLargeScale/generateVocabDict.py", line 20, in <module>
lines = getLines(drugDir)
File "/Users/stern/PycharmProjects/Stern/DTI/EssayLargeScale/generateVocabDict.py", line 8, in getLines
workbook = xlrd.open_workbook(os.path.join(dir,file))
File "/Users/stern/miniconda3/envs/env_rdkit/lib/python2.7/site-packages/xlrd/__init__.py", line 157, in open_workbook
ragged_rows=ragged_rows,
File "/Users/stern/miniconda3/envs/env_rdkit/lib/python2.7/site-packages/xlrd/book.py", line 92, in open_workbook_xls
biff_version = bk.getbof(XL_WORKBOOK_GLOBALS)
File "/Users/stern/miniconda3/envs/env_rdkit/lib/python2.7/site-packages/xlrd/book.py", line 1278, in getbof
bof_error('Expected BOF record; found %r' % self.mem[savpos:savpos+8])
File "/Users/stern/miniconda3/envs/env_rdkit/lib/python2.7/site-packages/xlrd/book.py", line 1272, in bof_error
raise XLRDError('Unsupported format, or corrupt file: ' + msg)
xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Expected BOF record; found '\x05stern '
Process finished with exit code 1
源代码:
import xlrd
import os
def getLines(dir):
lines = []
for filein [_for _in os.listdir(dir)if _[0]!='.']:#这里解决问题,原因是打开了目录下的临时文件 “.~Enzyme_Drug_Structure.xlsx”
workbook = xlrd.open_workbook(os.path.join(dir,file))
sheet = workbook.sheet_by_index(0)
linesOfFile = sheet.col_values(1)
linesOfFile = [x.encode('utf-8')for xin linesOfFile]
lines.append(linesOfFile)
return lines
def writeToTxt(lines,filepath,filename):
with open(os.path.join(filepath,filename),'w')as f:
for linein lines:
f.write(line)
drugDir ='/Users/stern/Desktop/Dataset/LargeScale/DictFile/Drug_smi'
lines = getLines(drugDir)
writeToTxt(lines,drugDir,'drugLines.txt')