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

Tôi có một tệp văn bản với mã hóa UTF-8. Tôi muốn thay đổi nó là Unicode thành ANSI hoặc Unicode tự động trong Python. Có thể không? Tôi làm nó như thế nào?

Hỏi ngày 25 tháng 12 năm 2016 lúc 9:50Dec 25, 2016 at 9:50Dec 25, 2016 at 9:50

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

5

Thử cái này

#read input file
with codecs.open('USERS.CSV', 'r', encoding = 'latin-1') as file:
lines = file.read()  

#write output file
with codecs.open('1_UserPython.CSV', 'w', encoding = 'utf_8_sig') as file:
file.write(lines)

Đã trả lời ngày 24 tháng 8 năm 2017 lúc 17:52Aug 24, 2017 at 17:52Aug 24, 2017 at 17:52

Để chuyển đổi một tệp từ UTF8 sang CP1252:

import io

with io.open(src_path, mode="r", encoding="utf8") as fd:
    content = fd.read()
with io.open(dst_path, mode="w", encoding="cp1252") as fd:
    fd.write(content)

Đã trả lời ngày 25 tháng 12 năm 2016 lúc 10:47Dec 25, 2016 at 10:47Dec 25, 2016 at 10:47

Laurent Laportelaurent LaporteLaurent LAPORTELaurent LAPORTE

20.6K5 Huy hiệu vàng54 Huy hiệu bạc95 Huy hiệu Đồng5 gold badges54 silver badges95 bronze badges5 gold badges54 silver badges95 bronze badges

Ví dụ này là để đáp ứng câu hỏi hỗ trợ của người dùng cụ thể:

Tôi có một tệp chứa văn bản này:

Văn bản tệp gốc Chuyển đổi bằng Notepad
Chữ Cafã Café
Hex 43 41 46 C3 89 43 41 46 C9

Biểu diễn UTF-8 của ký tự é là hai byte 0xc3 0x89. Khi Notepad hiển thị tệp UTF-8, nó đang định nghĩa các byte như thể chúng là ANSI (1 byte mỗi char), và do đó nó hiển thị ANSI char cho 0xc3 (ã) và ANSI char cho 0x89 (‰). Sau khi chuyển đổi sang ANSI, é được biểu thị bằng byte đơn 0xc9.

import sys
import chilkat

# This example assumes the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

charset = chilkat.CkCharset()

charset.put_FromCharset("utf-8")
charset.put_ToCharset("ANSI")

# We could alternatively be more specific and say "Windows-1252".
# The term "ANSI" means -- whatever character encoding is defined as the ANSI
# encoding for the computer.  In Poland, for example, it would be the single-byte-per-char
# used to represnt Eastern European language chars, which is Windows-1250.
charset.put_ToCharset("Windows-1252")

success = charset.ConvertFile("qa_data/txt/cafeUtf8.txt","qa_output/cafeAnsi.txt")
if (success != True):
    print(charset.lastErrorText())
    sys.exit()

print("Success.")

Thử cái này

#read input file
with codecs.open('USERS.CSV', 'r', encoding = 'latin-1') as file:
   lines = file.read()

#write output file
with codecs.open('1_UserPython.CSV', 'w', encoding = 'utf_8_sig') as file:
   file.write(lines)

Để chuyển đổi một tệp từ UTF8 sang CP1252:

import io

with io.open(src_path, mode = "r", encoding = "utf8") as fd:
   content = fd.read()
with io.open(dst_path, mode = "w", encoding = "cp1252") as fd:
   fd.write(content)

Đã trả lời ngày 25 tháng 12 năm 2016 lúc 10:47Dec 25, 2016 at 10:47

Laurent Laportelaurent LaporteLaurent LAPORTE

20.6K5 Huy hiệu vàng54 Huy hiệu bạc95 Huy hiệu Đồng5 gold badges54 silver badges95 bronze badges

t = word.get() #I 'm using tkinter, word is an entry field
e = meaning.get() #I 'm using tkinter, meaning is an entry field
meaning.delete(0, END)
word.focus()
es = e.encode("utf-8")
ts = t.encode("utf-8")
es.decode("utf-8")
ts.decode("utf-8")

#then the usual write procedure whre I write es and ts to the file.

Ví dụ này là để đáp ứng câu hỏi hỗ trợ của người dùng cụ thể:

fp = codecs.open('test', encoding = 'utf-8', mode = 'w+')
fp.write(u '\u4500 blah blah blah\n')

Ví dụ này là để đáp ứng câu hỏi hỗ trợ của người dùng cụ thể:

fp = codecs.open('test', encoding = 'utf-8', mode = 'w+')
fp.write(u '\u4500 blah blah blah\n')

Tôi có một tệp chứa văn bản này:

Văn bản tệp gốc

Chuyển đổi bằng Notepad

import io

src_path = (IN[0])

with io.open(src_path, mode = "r", encoding = "Unicode") as fd:
   content = fd.read()
with io.open(dst_path, mode = "w", encoding = "ANSI") as fd:
   fd.write(content)

Chữ
Just stumbled upon your issue.
In order to import libraries form the IronPython lib you need to add a reference to the location in your python script, so in order to use “io” you should add this line above “import io”:

import sys
sys.path.append("C:\Program Files (x86)\IronPython 2.7\Lib")

Cafã

import io

with io.open(src_path, mode="r", encoding="utf8") as fd:
    content = fd.read()
with io.open(dst_path, mode="w", encoding="cp1252") as fd:
    fd.write(content)
0

Gợi ý: 4

Các codec sau đây cung cấp mã hóa STR cho byte và đối tượng giống như byte để giải mã STR, tương tự như mã hóa văn bản Unicode., Mã hóa chỉ định mã hóa được sử dụng cho tệp. Bất kỳ mã hóa nào mã hóa và giải mã từ byte đều được cho phép và các loại dữ liệu được hỗ trợ bởi các phương thức tệp phụ thuộc vào codec được sử dụng., Mô-đun này thực hiện một biến thể của codec UTF-8. Khi mã hóa, BOM được mã hóa UTF-8 sẽ được chuẩn bị cho UTF-8 được mã hóa byte. Đối với bộ mã hóa trạng thái, điều này chỉ được thực hiện một lần (trên lần ghi đầu tiên vào luồng byte). Khi giải mã, một BOM được mã hóa UTF-8 tùy chọn khi bắt đầu dữ liệu sẽ được bỏ qua., Đặt lại bộ mã hóa về trạng thái ban đầu. Đầu ra bị loại bỏ: Call .enCode (Object, Final = true), chuyển một byte trống hoặc chuỗi văn bản nếu cần thiết, để đặt lại bộ mã hóa và để lấy đầu ra.

import io

with io.open(src_path, mode="r", encoding="utf8") as fd:
    content = fd.read()
with io.open(dst_path, mode="w", encoding="cp1252") as fd:
    fd.write(content)
1

Gợi ý: 5

Mô-đun Python cho Windows, Linux, Alpine Linux, Mac OS X, Solaris, FreeBSD, OpenBSD, Raspberry Pi và các máy tính bảng đơn khác, biểu diễn UTF-8 của ký tự é là hai byte 0xc3 0x89. Khi Notepad hiển thị tệp UTF-8, nó đang định nghĩa các byte như thể chúng là ANSI (1 byte mỗi char), và do đó nó hiển thị ANSI char cho 0xc3 (ã) và ANSI char cho 0x89 (‰). Sau khi chuyển đổi sang ANSI, é được biểu diễn bằng byte đơn 0xc9.

import io

with io.open(src_path, mode="r", encoding="utf8") as fd:
    content = fd.read()
with io.open(dst_path, mode="w", encoding="cp1252") as fd:
    fd.write(content)
2

Làm cách nào để thay đổi mã hóa từ UTF

Bước 1 - Mở tệp dữ liệu thô trong Notepad. Bước 2 - Chuyển đến menu Tệp; Chọn "Lưu dưới dạng". Bước 3 - Thay đổi tùy chọn mã hóa từ UTF -8 thành ANSI và lưu tệp của bạn. Bước 4 - Chọn OK khi cảnh báo về việc chuyển đổi thành mã hóa ANSI xuất hiện.

Mã hóa là gì = 'utf

UTF-8 là mã hóa theo định hướng byte. Mã hóa chỉ định rằng mỗi ký tự được biểu diễn bằng một chuỗi cụ thể của một hoặc nhiều byte.specifies that each character is represented by a specific sequence of one or more bytes.specifies that each character is represented by a specific sequence of one or more bytes.

Làm thế nào để bạn thay đổi UTF trong Python?

Làm thế nào để chuyển đổi một chuỗi thành UTF-8 trong Python ?...

String1 = "Apple" String2 = "preeti125" String3 = "12345" String4 = "[email protected]".

sợi dây.mã hóa (mã hóa = 'UTF-8', lỗi = 'nghiêm ngặt').

# chuỗi unicode chuỗi = 'pythön!'# Mã hóa mặc định thành UTF-8 String_utf = String.Encode () in ('Phiên bản được mã hóa là:', String_utf).

Là UTF

UTF-8 là một phương thức mã hóa ký tự Unicode.Điều này có nghĩa là UTF-8 lấy điểm mã cho một ký tự unicode đã cho và dịch nó thành một chuỗi nhị phân.Nó cũng làm ngược lại, đọc theo các chữ số nhị phân và chuyển đổi chúng thành các ký tự.. This means that UTF-8 takes the code point for a given Unicode character and translates it into a string of binary. It also does the reverse, reading in binary digits and converting them back to characters.. This means that UTF-8 takes the code point for a given Unicode character and translates it into a string of binary. It also does the reverse, reading in binary digits and converting them back to characters.