Hướng dẫn python ansi encoding - mã hóa python ansi


Tôi có khoảng 600.000 tệp được mã hóa trong ANSI và tôi muốn chuyển đổi chúng thành UTF-8. Tôi có thể làm điều đó riêng lẻ trong NOTEPAD++, nhưng tôi có thể làm điều đó với 600.000 tệp. Tôi có thể làm điều này trong ____10 hoặc

blockSize = 1048576with codecs.open["your ANSI source file.txt","r",encoding="mbcs"] as sourceFile: with codecs.open["Your UTF-8 output file.txt","w",encoding="UTF-8"] as targetFile: while True: contents = sourceFile.read[blockSize] if not contents: break targetFile.write[contents]
1 không?

Tôi đã tìm thấy liên kết này nhưng tập lệnh

blockSize = 1048576with codecs.open["your ANSI source file.txt","r",encoding="mbcs"] as sourceFile: with codecs.open["Your UTF-8 output file.txt","w",encoding="UTF-8"] as targetFile: while True: contents = sourceFile.read[blockSize] if not contents: break targetFile.write[contents]
1 không chạy: Notepad ++ chuyển đổi tệp được mã hóa ANSI sang UTF-8

hỏi ngày 17 tháng 7 năm 2015 lúc 8:05Jul 17, 2015 at 8:05


Hướng dẫn python ansi đến utf-8

6

Tại sao bạn không đọc tệp và viết nó là UTF-8? Bạn có thể làm điều đó trong Python.

#to support encodingsimport codecs#read input filewith codecs.open[path, 'r', encoding = 'utf8'] as file: lines = file.read[]#write output filewith codecs.open[path, 'w', encoding = 'utf8'] as file: file.write[lines]

Đã trả lời ngày 17 tháng 7 năm 2015 lúc 8:13Jul 17, 2015 at 8:13


Hướng dẫn python ansi đến utf-8

Tại sao bạn không đọc tệp và viết nó là UTF-8? Bạn có thể làm điều đó trong Python.3Ducker

#to support encodingsimport codecs#read input filewith codecs.open[path, 'r', encoding = 'utf8'] as file: lines = file.read[]#write output filewith codecs.open[path, 'w', encoding = 'utf8'] as file: file.write[lines]
1 silver badge9 bronze badges

3


Hướng dẫn python ansi đến utf-8

Tại sao bạn không đọc tệp và viết nó là UTF-8? Bạn có thể làm điều đó trong Python.

#to support encodingsimport codecs#read input filewith codecs.open[path, 'r', encoding = 'utf8'] as file: lines = file.read[]#write output filewith codecs.open[path, 'w', encoding = 'utf8'] as file: file.write[lines]

Đã trả lời ngày 17 tháng 7 năm 2015 lúc 8:13

blockSize = 1048576with codecs.open["your ANSI source file.txt","r",encoding="mbcs"] as sourceFile: with codecs.open["Your UTF-8 output file.txt","w",encoding="UTF-8"] as targetFile: while True: contents = sourceFile.read[blockSize] if not contents: break targetFile.write[contents]

3Ducker3Ducker

3231 Huy hiệu bạc9 Huy hiệu đồng

Tôi đánh giá cao rằng đây là một câu hỏi cũ nhưng vừa giải quyết một vấn đề tương tự gần đây, tôi nghĩ rằng tôi sẽ chia sẻ giải pháp của mình.Dec 19, 2018 at 17:27

Tôi đã có một tệp được chuẩn bị bởi một chương trình mà tôi cần nhập vào cơ sở dữ liệu SQLite3 nhưng tệp văn bản luôn là ‘ANSI, và SQLite3 yêu cầu UTF-8.

Mã hóa ANSI được công nhận là ‘MBCS, trong Python và do đó mã tôi đã sử dụng, xé toạc thứ khác tôi tìm thấy là:

Liên kết dưới đây chứa một số thông tin về các loại mã hóa mà tôi tìm thấy trong nghiên cứu của mình

//docs.python.org/2.4/lib/standard-encodings.html

Đã trả lời ngày 19 tháng 12 năm 2018 lúc 17:27

cp874 Thaicp932 Japanese cp936 Unified Chinese [P.R. China, Singapore]cp949 Korean cp950 Traditional Chinese [Taiwan, Hong Kong, Macao[?]]cp1250 Central and Eastern Europe cp1251 Cyrillic [ Belarusian, Bulgarian, Macedonian, Russian, Serbian, Ukrainian]cp1252 Western European languagescp1253 Greek cp1254 Turkish cp1255 Hebrew cp1256 Arabic scriptcp1257 Baltic languages cp1258 Vietnamesecp???? languages/scripts of India 

MS Notepad cung cấp cho người dùng lựa chọn 4 mã hóa, được thể hiện bằng thuật ngữ khó hiểu vụng về:

UN UNICode là UTF-16, viết Little-endian. Unicode Unicode Big Endian là UTF-16, được viết lớn. Trong cả hai trường hợp UTF-16, điều này có nghĩa là BOM thích hợp sẽ được viết. Sử dụng

blockSize = 1048576with codecs.open["your ANSI source file.txt","r",encoding="mbcs"] as sourceFile: with codecs.open["Your UTF-8 output file.txt","w",encoding="UTF-8"] as targetFile: while True: contents = sourceFile.read[blockSize] if not contents: break targetFile.write[contents]
3 để giải mã một tệp như vậy.

UTF-8 là UTF-8; Notepad viết rõ ràng một BOM UTF-8. Sử dụng

blockSize = 1048576with codecs.open["your ANSI source file.txt","r",encoding="mbcs"] as sourceFile: with codecs.open["Your UTF-8 output file.txt","w",encoding="UTF-8"] as targetFile: while True: contents = sourceFile.read[blockSize] if not contents: break targetFile.write[contents]
4 để giải mã một tệp như vậy.

Một số người gây sốc. Đây là thuật ngữ MS cho bất cứ điều gì mã hóa di sản mặc định trên máy tính này.

The quick brown fox jumped over the lazy dogs.àáâãäå

Dưới đây là danh sách các mã hóa Windows mà tôi biết và các ngôn ngữ/tập lệnh mà chúng được sử dụng cho:

import localedef guess_notepad_encoding[filepath, default_ansi_encoding=None]: with open[filepath, 'rb'] as f: data = f.read[3] if data[:2] in ['xffxfe', 'xfexff']: return 'utf-16' if data == u''.encode['utf-8-sig']: return 'utf-8-sig' # presumably "ANSI" return default_ansi_encoding or locale.getpreferredencoding[]if __name__ == "__main__": import sys, glob, codecs defenc = sys.argv[1] for fpath in glob.glob[sys.argv[2]]: print print [fpath, defenc] with open[fpath, 'rb'] as f: print "raw:", repr[f.read[]] enc = guess_notepad_encoding[fpath, defenc] print "guessed encoding:", enc with codecs.open[fpath, 'r', enc] as f: for lino, line in enumerate[f, 1]: print lino, repr[line] print lino, repr[line.rstrip['rn']]

Nếu tệp đã được tạo trên máy tính nơi nó đang được đọc, thì bạn có thể có được mã hóa ANSI ANSI bằng cách

blockSize = 1048576with codecs.open["your ANSI source file.txt","r",encoding="mbcs"] as sourceFile: with codecs.open["Your UTF-8 output file.txt","w",encoding="UTF-8"] as targetFile: while True: contents = sourceFile.read[blockSize] if not contents: break targetFile.write[contents]
5. Mặt khác, nếu bạn biết nó đến từ đâu, bạn có thể chỉ định mã hóa để sử dụng nếu nó không phải là UTF-16. Thất bại điều đó, đoán.

['t1-ansi.txt', '']raw: 'The quick brown fox jumped over the lazy dogs.rnxe0xe1xe2xe3xe4xe5rn'guessed encoding: cp12521 u'The quick brown fox jumped over the lazy dogs.rn'1 u'The quick brown fox jumped over the lazy dogs.'2 u'xe0xe1xe2xe3xe4xe5rn'2 u'xe0xe1xe2xe3xe4xe5'['t1-u8.txt', '']raw: 'xefxbbxbfThe quick brown fox jumped over the lazy dogs.rnxc3xa0xc3xa1xc3xa2xc3xa3xc3xa4xc3xa5rn'guessed encoding: utf-8-sig1 u'The quick brown fox jumped over the lazy dogs.rn'1 u'The quick brown fox jumped over the lazy dogs.'2 u'xe0xe1xe2xe3xe4xe5rn'2 u'xe0xe1xe2xe3xe4xe5'['t1-uc.txt', '']raw: 'xffxfeTx00hx00ex00 x00qx00ux00ix00cx00kx00 x00bx00rx00ox00wx00nx00 x00fx00ox00xx00 x00jx00ux00mx00px00ex00dx00 x00ox00vx00ex00rx00 x00tx00hx00ex00 x00lx00ax00zx00yx00 x00dx00ox00gx00sx00.x00rx00nx00xe0x00xe1x00xe2x00xe3x00xe4x00xe5x00rx00nx00'guessed encoding: utf-161 u'The quick brown fox jumped over the lazy dogs.rn'1 u'The quick brown fox jumped over the lazy dogs.'2 u'xe0xe1xe2xe3xe4xe5rn'2 u'xe0xe1xe2xe3xe4xe5'['t1-ucb.txt', '']raw: 'xfexffx00Tx00hx00ex00 x00qx00ux00ix00cx00kx00 x00bx00rx00ox00wx00nx00 x00fx00ox00xx00 x00jx00ux00mx00px00ex00dx00 x00ox00vx00ex00rx00 x00tx00hx00ex00 x00lx00ax00zx00yx00 x00dx00ox00gx00sx00.x00rx00nx00xe0x00xe1x00xe2x00xe3x00xe4x00xe5x00rx00n'guessed encoding: utf-161 u'The quick brown fox jumped over the lazy dogs.rn'1 u'The quick brown fox jumped over the lazy dogs.'2 u'xe0xe1xe2xe3xe4xe5rn'2 u'xe0xe1xe2xe3xe4xe5'

Hãy cẩn thận bằng cách sử dụng

blockSize = 1048576with codecs.open["your ANSI source file.txt","r",encoding="mbcs"] as sourceFile: with codecs.open["Your UTF-8 output file.txt","w",encoding="UTF-8"] as targetFile: while True: contents = sourceFile.read[blockSize] if not contents: break targetFile.write[contents]
6 để đọc các tệp trên Windows. Các tài liệu cho biết: Các tệp ghi chú của người Hồi giáo luôn được mở ở chế độ nhị phân, ngay cả khi không có chế độ nhị phân nào được chỉ định. Điều này được thực hiện để tránh mất dữ liệu do mã hóa sử dụng các giá trị 8 bit. Điều này có nghĩa là không có sự chuyển đổi tự động của ‘n, được thực hiện khi đọc và viết.

Để tất cả chúng cùng nhau:

>>> all_bytes = "".join[map[chr, range[256]]]>>> u1 = all_bytes.decode['cp1252', 'replace']>>> u2 = all_bytes.decode['mbcs', 'replace']>>> u1 == u2False>>> [[i, u1[i], u2[i]] for i in xrange[256] if u1[i] != u2[i]][[129, u'ufffd', u'x81'], [141, u'ufffd', u'x8d'], [143, u'ufffd', u'x8f'], [144, u'ufffd', u'x90'], [157, u'ufffd', u'x9d']]>>>

Tệp văn bản mẫu, được lưu với cả 4 lựa chọn mã hóa, có vẻ như thế này trong Notepad:

Bài Viết Liên Quan

Chủ Đề