Python batch conversion file encoding format

Source: Internet
Author: User

Python batch conversion file encoding format

You need to transcode the files in the working directory. The encoding starts from GBK and needs to be converted to UTF-8. There are many files, but manual conversion is definitely not feasible. Use Python to write a script for implementation.

Self-written method, applicable to linux,

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

#! /Usr/bin/python

# Coding = UTF-8

Import sys

Import OS, OS. path

Import dircache

Import commands

Def add (x, y ):

Return x * y

 

Def trans (dirname ):

Lis = dircache. opendir (dirname)

For a in lis:

Af = dirname + OS. sep +

# Print af

If OS. path. isdir (af ):

# Print af

Trans (af)

Else:

# Print af + "encoding =" + fi. name

Ft = commands. getoutput ('file-I '+ af)

# Print ft

If a.find('.htm') =-1 and a. find ('. xml') =-1 and ft. find ('text /')! =-1 and ft. find ('iso-8859 ')! =-1:

Print 'gbk' + ft + ">" + af

Commands. getoutput ('iconv-ficonv-f gbk-t UTF-8-c-O' + "" + af + "" + af)

 

Trans (OS. getcwd ())

Available code for versions earlier than py2.6

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

Import OS, sys

 

Def convert (filename, in_enc = "GBK", out_enc = "UTF8 "):

Try:

Print "convert" + filename,

Content = open (filename). read ()

New_content = content. decode (in_enc). encode (out_enc)

Open (filename, 'w'). write (new_content)

Print "done"

Except t:

Print "error"

 

Def inclue (dir ):

For root, dirs, files in OS. walk (dir ):

For file in files:

Path = OS. path. join (root, file)

Convert (path)

 

Def main ():

For path in sys. argv [1:]:

If OS. path. isfile (path ):

Convert (path)

Elif OS. path. isdir (path ):

Explore (path)

 

If _ name _ = "_ main __":

Main ()

Py3.1 version is supported.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

Import OS

Import sys

Import codecs

# This program is used to convert files in the directory from the specified format to the specified format. The default value is GBK to UTF-8.

Def convert (file, in_enc = "GBK", out_enc = "UTF-8 "):

Try:

Print ("convert" + file)

F = codecs. open (file, 'R', in_enc)

New_content = f. read ()

Codecs. open (file, 'w', out_enc). write (new_content)

# Print (f. read ())

Handle t IOError as err:

Print ("I/O error: {0}". format (err ))

 

 

Def inclue (dir ):

For root, dirs, files in OS. walk (dir ):

For file in files:

Path = OS. path. join (root, file)

Convert (path)

 

Def main ():

For path in sys. argv [1:]:

If (OS. path. isfile (path )):

Convert (path)

Elif OS. path. isdir (path ):

Explore (path)

 

If _ name __= = "_ main __":

Main ()

The above is all the content of this article. I hope you will like it.

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.