问题描述:
在python3中运行的程序中包含如下内容:
fp = file(s, 'rb')
使用python3的环境运行之后显示如下;
Traceback (most recent call last):
File "extract_para.py", line 23, in <module>
run()
File "extract_para.py", line 10, in run
text=pf.preprocess()
File "C:\Users\cheng\Desktop\para\preprocess_function.py", line 8, in preprocess
html_file=turn(pdf_path)
File "C:\Users\cheng\Desktop\para\convert.py", line 45, in turn
outfp = file("output.html", 'w')
NameError: name 'file' is not defined
错误原因:python3移除了file()方法
改进方法:
把file()方法改成open()方法。
又出现了错误:
Traceback (most recent call last):
File "extract_para.py", line 23, in <module>
run()
File "extract_para.py", line 10, in run
text=pf.preprocess()
File "C:\Users\cheng\Desktop\para\preprocess_function.py", line 7, in preprocess
html_file=turn(pdf_path)
File "C:\Users\cheng\Desktop\para\convert.py", line 53, in turn
imagewriter=imagewriter)
File "C:\RRRRRR-Anaconda3\envs\cheng\lib\site-packages\pdfminer\converter.py", line 268, in __init__
self.write_header()
File "C:\RRRRRR-Anaconda3\envs\cheng\lib\site-packages\pdfminer\converter.py", line 278, in write_header
self.write('<html><head>\n')
File "C:\RRRRRR-Anaconda3\envs\cheng\lib\site-packages\pdfminer\converter.py", line 274, in write
self.outfp.write(text)
TypeError: write() argument must be str, not bytes
改进方法:
f = open('draft.txt','w') --------------> f = open('draft.txt','wb')
f = open('draft.txt','r') --------------> f = open('draft.txt','rb')
bingo!