Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
To help the gf, I'm trying to make a python program to get a lot of data from a webpage and write it to excel. I got one set of data, but the encoding seems to be GB2312
I never got iconv to work. I just tried an example from its man page: it returned
Quote:
illegal input sequence at position 4
- so much for documentation. I use utf8trans instead. The drawback to that is that the provided tables were incomplete so I had to add entries for new characters. Perhaps I could have found better tables had I searched. A search on gb2312 on my computer turned up some python stuff that looks like it's meant to handle it, perhaps convert to/from UTF.
The first argument to utf8trans is a character map. A character map is a 2-column file, the first column hex characters in utf-8, the second target characters, separated by a tab. Unfortunately GB2312.gz is not in the correct format for utf8trans. I don't remember, but I suspect I created my own character maps. I'd hope there's one for GB2312 somewhere but I don't know where. I'd also hope that i18n's character map would serve some translation utility that would do the same. If it were up to me I'd write a program to translate i18n's character map to utf8trans format, but that's because I've already made utf8trans work for other purposes. Whether that'd be the best use of your time is a different question.
I don't use python but it looks like it has translation facilities for utf & gb2312 in it already. Perhaps a python-knowledgeable person would know.
How did you originally convert the GB2313 encoded file to UTF8 in Python?
I got the data from the webpage with:
Quote:
line = soup.find('table').text
then I just opened a file, wrote line to the file, closed the file. I presume Python default is to write UTF-8. I think what it was writing was encoded GB2312
Quote:
data = open(path + 'page1', 'w')
data.write(line)
data.close()
The link to stackoverflow is very promising. Thank you very much!
Don't know why it will not accept a string, but that can be tweaked I think! Progress! Thanks!
I just tried this in my Python terminal, and I get Chinese:
Quote:
data = '»Æ¹ûÊ÷'
data.encode('latin1').decode('gb2312')
'黄果树'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.