Skip to content

to_html()没有encoding参数?造成输出中文显示乱码 #28663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
645187919 opened this issue Sep 28, 2019 · 6 comments · Fixed by #28692
Closed

to_html()没有encoding参数?造成输出中文显示乱码 #28663

645187919 opened this issue Sep 28, 2019 · 6 comments · Fixed by #28692
Labels
IO HTML read_html, to_html, Styler.apply, Styler.applymap
Milestone

Comments

@645187919
Copy link

Code Sample, a copy-pastable example if possible

# Your code here
data = pd.read_csv("test.csv",encoding="gbk")
print(data)
html = data.to_html(col_space=40,justify="center")

Problem description

当我用to_html()方法生成html的时候,中文显示乱码,后来一看该方法竟然没有encoding参数,希望作者能完善了,这样以后会方便很多人哈!
[this should explain why the current behaviour is a problem and why the expected output is a better solution.]

Note: We receive a lot of issues on our GitHub tracker, so it is very possible that your issue has been posted before. Please check first before submitting so that we do not have to handle and close duplicates!

Note: Many problems can be resolved by simply upgrading pandas to the latest version. Before submitting, please check if that solution works for you. If possible, you may want to check if master addresses this issue, but that is not necessary.

For documentation-related issues, you can check the latest versions of the docs on master here:

https://pandas-docs.github.io/pandas-docs-travis/

If the issue has not been resolved there, go ahead and file it in the issue tracker.

Expected Output

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]

@simonjayhawkins
Copy link
Member

@645187919 Thanks for the report, however, can you provide a reproducible example. http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports

@simonjayhawkins simonjayhawkins added IO HTML read_html, to_html, Styler.apply, Styler.applymap Needs Info Clarification about behavior needed to assess issue IO CSV read_csv, to_csv labels Sep 28, 2019
@charlesdong1991
Copy link
Member

charlesdong1991 commented Sep 29, 2019

I think he is saying there is no encoding argument in to_html, and this will cause an issue when exporting contents including Chinese characters. @simonjayhawkins

@simonjayhawkins
Copy link
Member

Thanks @charlesdong1991 .

the to_html method has no encoding. I assume that it is fairly simple to work-around by passing an open buffer with the correct encoding or not passing a filepath, and writing the return unicode string to file using the appropriate encoding?

the buffer handling code is shared with to_latex and to_string.

to_latex already has an encoding parameter and the shared code can handle encoding.

so it should be straightforward to add encoding to to_html ( and to_string) for consistency.

#15008 (comment) "Textual payloads should support the encoding argument"

#28377 has been opened for discussion regarding passing options for the open call.

but I think in this case, for consistency with other IO methods, we could just add the encoding argument directly to the to_html argument list. @TomAugspurger

@simonjayhawkins simonjayhawkins removed IO CSV read_csv, to_csv Needs Info Clarification about behavior needed to assess issue labels Sep 29, 2019
@charlesdong1991
Copy link
Member

yeah, agree, a solution to solve this issue could be adding encoding to to_html in io/formats/format.py @simonjayhawkins
And a separate PR could be making it sharable among those three methods.

@cailurus
Copy link
Contributor

added at #28692

@mohitanand001
Copy link
Contributor

Added at #28951

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO HTML read_html, to_html, Styler.apply, Styler.applymap
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants