The problem that fgetcsv cannot parse Chinese Characters in Ubuntu10.10

Source: Internet
Author: User
This (http://www.linuxidc.com/Linux/2011-08/40394p2.htm) describes the use of the fgets method to replace fgetcsv to read the csv file, and later found that if not directly replaced by the processing, there is still a problem: really make me depressed for a long time ah. First, fgetcsv correctly processes the content in a row during processing. Even if the row contains line breaks and line breaks, it can be processed and parsed correctly. However, if you use fgets, you need to do this by yourself.

This (http://www.linuxidc.com/Linux/2011-08/40394p2.htm) describes the use of the fgets method to replace fgetcsv to read the csv file, and later found that if not directly replaced by the processing, there is still a problem: really make me depressed for a long time ah.

First, fgetcsv correctly processes the content in a row during processing. Even if the row contains line breaks and line breaks, it can be processed and parsed correctly. If you use fgets, you need to do this by yourself. Of course, a method to rewrite fgetcsv is also provided on the Internet. Of course, you need to consider the efficiency of using regular expressions.

However, fgetcsv has a problem. As mentioned in the previous article, the region settings, of course, setlocale (LC_ALL, 'zh _ cn'). It turns out that it can be solved, however, this is a test in WIN. The tragedy occurred when I moved to Ubuntu for testing.

Although my ubuntu system uses the Chinese language, the local code is zh_CN.UTF8, and there is no zh_CN.GBK. Therefore, the Chinese text cannot be parsed.

Of course, the shoulders of the predecessors are always great. With the help of the Internet, everything can be solved.

First, I updated the Language Pack, system-> Management-> language support. When I started it for the first time, I was prompted that I didn't install it completely and needed to upgrade it. So I upgraded it, but when I checked locale-a in the command line, there was still no zh_CN:

Www.linuxidc.com @ ubuntu:/usr/www/GBKHost/test $ locale-
C
POSIX
Zh_CN.utf8
Zh_HK.utf8
Zh_SG.utf8
Zh_TW.utf8

So I did this:

1. Modify the local file and add zh_CN support:

Sudo vi/var/lib/locales/supported. d/local

Insert a row of zh_CN in front of the first row. In this case, I also inserted the row zh_CN.GBK GBK.

Save.

2. Set the support package

Sudo locale-gen zh_CN

Sudo locale-gen zh_CN.GBK

3. Run locale-a again.

Www.linuxidc.com @ ubuntu:/usr/www/GBKHost/test $ locale-

C

POSIX

Zh_CN

Zh_CN.gb2312

Zh_CN.gbk

Zh_CN.utf8

Zh_HK.utf8

Zh_SG.utf8

Zh_TW.utf8

Finally.

Run the csv import page again (including setlocale (LC_CTYPE, 'zh _ cn!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.