2017-10-12 1 views
1

pandashere으로 생성 된 테이블을 PDF로 내보내는 좋은 방법을 발견했습니다.이 파일을 png 파일로 변환하는 부분은 저에게 흥미가 없습니다.pandas 테이블을 pdf로 내보내기

문제는 내가 다음과 같은 오류 메시지가 얻을입니다 : 정말 꽤 하드 오류를 수정하게 처음부터 코드를 이해하고 있지 않다

--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-10-9818a71c26bb> in <module>() 
    13 
    14 with open(filename, 'wb') as f: 
---> 15  f.write(template.format(z.to_latex())) 
    16 
    17 subprocess.call(['pdflatex', filename]) 

TypeError: a bytes-like object is required, not 'str' 

합니다. 내 코드는 다음과 같습니다

import subprocess 

filename = 'out.tex' 
pdffile = 'out.pdf' 

template = r'''\documentclass[preview]{{standalone}} 
\usepackage{{booktabs}} 
\begin{{document}} 
{} 
\end{{document}} 
''' 

with open(filename, 'wb') as f: 
    f.write(template.format(z.to_latex())) 

subprocess.call(['pdflatex', filename]) 

zpandas으로 생성 된 DataFrame이다.

누군가가 도와 줄 수 있기를 바랍니다. 미리 감사드립니다. 시토.

답변

0

문제는 파일을 열어 바이트 모드로 작성한다는 것입니다. 즉, "b"문자가 open()에 대한 호출에서 의미하는 것입니다. 그런 다음 문자열 데이터를 전달합니다. 이 변경 :

with open(filename, 'wb') as f: 
    f.write(template.format(z.to_latex())) 

을 여기에 :

with open(filename, 'w') as f: 
    f.write(template.format(z.to_latex()))