2012-04-23 19 views
15

में एक गैर UTF-8 पाठ फ़ाइल पढ़ना मुझे लगता है कि GBK में एन्कोड किया गया है एक पाठ फ़ाइल पढ़ने की जरूरत है। गो प्रोग्रामिंग भाषा में मानक लाइब्रेरी मानती है कि सभी पाठ यूटीएफ -8 में एन्कोड किए गए हैं।जाओ

मैं अन्य एन्कोडिंग में फ़ाइलों को कैसे पढ़ सकता हूं?

+0

क्यों downvotes? –

+0

यह एक बहुत ही उचित और स्पष्ट प्रश्न जैसा दिखता है, फिर से खोलने के लिए मतदान (इसके मूल रूप में भी) – OscarRyz

उत्तर

13

पहले (के रूप में एक पुराने जवाब में बताया गया) "आसान" यह करने के लिए जिस तरह से तीसरे पक्ष संकुल कि सीजीओ की जरूरत है और iconv पुस्तकालय लिपटे का उपयोग कर शामिल किया गया। यह कई कारणों से अवांछनीय है। शुक्र है, काफी समय से गो लेखकों द्वारा प्रदान किए गए केवल पैकेजों का उपयोग करके ऐसा करने का एक बेहतर तरीका है (संकुल के मुख्य सेट में नहीं बल्कि Go Sub-Repositories में)।

golang.org/x/text/encoding पैकेज सामान्य वर्ण एन्कोडिंग कि UTF-8 से/करने के लिए परिवर्तित कर सकते हैं के लिए एक इंटरफेस को परिभाषित करता है। golang.org/x/text/encoding/simplifiedchinese उप-पैकेज GB18030, GBK और HZ-GB2312 एन्कोडिंग कार्यान्वयन प्रदान करता है।

यहां एक जीबीके एन्कोडेड फ़ाइल पढ़ने और लिखने का एक उदाहरण है। ध्यान दें कि io.Reader और io.Writer डेटा को "फ्लाई पर" एन्कोडिंग करें क्योंकि डेटा पढ़ा/लिखा जा रहा है।

package main 

import (
    "bufio" 
    "fmt" 
    "log" 
    "os" 

    "golang.org/x/text/encoding/simplifiedchinese" 
    "golang.org/x/text/transform" 
) 

// Encoding to use. Since this implements the encoding.Encoding 
// interface from golang.org/x/text/encoding you can trivially 
// change this out for any of the other implemented encoders, 
// e.g. `traditionalchinese.Big5`, `charmap.Windows1252`, 
// `korean.EUCKR`, etc. 
var enc = simplifiedchinese.GBK 

func main() { 
    const filename = "example_GBK_file" 
    exampleWriteGBK(filename) 
    exampleReadGBK(filename) 
} 

func exampleReadGBK(filename string) { 
    // Read UTF-8 from a GBK encoded file. 
    f, err := os.Open(filename) 
    if err != nil { 
     log.Fatal(err) 
    } 
    r := transform.NewReader(f, enc.NewDecoder()) 

    // Read converted UTF-8 from `r` as needed. 
    // As an example we'll read line-by-line showing what was read: 
    sc := bufio.NewScanner(r) 
    for sc.Scan() { 
     fmt.Printf("Read line: %s\n", sc.Bytes()) 
    } 
    if err = sc.Err(); err != nil { 
     log.Fatal(err) 
    } 

    if err = f.Close(); err != nil { 
     log.Fatal(err) 
    } 
} 

func exampleWriteGBK(filename string) { 
    // Write UTF-8 to a GBK encoded file. 
    f, err := os.Create(filename) 
    if err != nil { 
     log.Fatal(err) 
    } 
    w := transform.NewWriter(f, enc.NewEncoder()) 

    // Write UTF-8 to `w` as desired. 
    // As an example we'll write some text from the Wikipedia 
    // GBK page that includes Chinese. 
    _, err = fmt.Fprintln(w, 
     `In 1995, China National Information Technology Standardization 
Technical Committee set down the Chinese Internal Code Specification 
(Chinese: 汉字内码扩展规范(GBK); pinyin: Hànzì Nèimǎ 
Kuòzhǎn Guīfàn (GBK)), Version 1.0, known as GBK 1.0, which is a 
slight extension of Codepage 936. The newly added 95 characters were not 
found in GB 13000.1-1993, and were provisionally assigned Unicode PUA 
code points.`) 
    if err != nil { 
     log.Fatal(err) 
    } 

    if err = f.Close(); err != nil { 
     log.Fatal(err) 
    } 
} 
5

go-iconv आज़माएं। यह iconv लपेटता है और io.Reader और io.Writer लागू करता है।

गोलांग-चीन चर्चा समूह में यह messagego-iconv उपयोग के कुछ उदाहरणों का उल्लेख कर रहा है।

+0

एक और विकल्प फ़ाइल को एक बफर में एक अपारदर्शी ब्लॉब के रूप में पढ़ना होगा और फिर इसे किसी विशेष एन्कोडिंग में टेक्स्ट के रूप में समझना होगा [go-charset] का उपयोग कर (http://code.google.com/p/go-charset); अधिक जानकारी के लिए [यह सवाल] देखें (http://stackoverflow.com/q/10039701/720999)। मैंने कोड को नहीं देखा, लेकिन ऐसा लगता है कि सबसे अधिक उपयोग किए जाने वाले विरासत वर्णमाला/एन्कोडिंग के सेट का समर्थन करने के लिए, इस पैकेज को आइकनव की आवश्यकता नहीं है जो एक लाभ हो सकता है। – kostix

+0

@kostix http://code.google.com/p/go-charset के शीर्षक पृष्ठ पर जानकारी से ऐसा लगता है कि gbk के मामले में पैकेज GNU iconv लाइब्रेरी का उपयोग करता है। –

+0

इस पैकेज का उपयोग रूपांतरण करने के लिए भी किया जा सकता है: https://godoc.org/golang.org/x/text/encoding – OscarRyz

संबंधित मुद्दे