Python 字符串编码 xmlcharrefreplace 解码
Python string encoding xmlcharrefreplace decode
我正在使用 xmlcharrefreplace
替换字符串中的非标准字符,以便可以将其保存在 xml 文件中。稍后,我想将此字符串重新转换回原始字符。
import openpyxl
import cgi
from html.parser import HTMLParser
parser = HTMLParser()
startingString = "Tỉnh Đồng Nai" #example string
print("Starting string: " + startingString) #Starting string: Tỉnh Đồng Nai
# 1. This string contains non-standard characters. Convert these characters using xmlcharrefreplace
escapedString = cgi.escape(startingString)
strEscapedString = str(escapedString)
aposString = strEscapedString.replace("'", "'")
savedToExcelString = str(aposString.encode('utf-8', 'xmlcharrefreplace') )[2:-1]
print("xmlcharrefreplace converted to: " + savedToExcelString) #xmlcharrefreplace converted to: T\xe1\xbb\x89nh \xc4\x90\xe1\xbb\x93ng Nai
# 2. The string is saved to an xml file
# 3. The string is read from an xml file
# 4. Convert the string back into the original starting string
unescapedString = parser.unescape(savedToExcelString)
#what do I do here??? I need to 'undo' the xmlcharrefreplace encoding
print(startingString + " == " + unescapedString + " is " + str(startingString == unescapedString))
# Tỉnh Đồng Nai == T\xe1\xbb\x89nh \xc4\x90\xe1\xbb\x93ng Nai is False
# ^^ Should be the same string at the end
请注意,我不能使用 codecs.open(),因为我正在使用库 openpyxl 打开一个包含数据的 Excel 文件。输入的字符集没有限制 - 我希望最终字符串与初始字符串相同。
目标:将字符从 xmlcharrefreplace 转换回其脚本字符。
例如:“\x90”变成“ồ”
您所描述的是不可能的,一般来说。当您从一个大字符集(任何)转换为一个较小的字符集时,您会丢失信息。除非您对编码结果有限制,否则您无法编码可以逆转的东西。
但是,如果您与数据库有一些约定,这些标签将不会出现在输入中(例如字符序列“\x”),您可以使用其中一个或多个表示编码字符串,您可以在该区域创建您喜欢的任何代码。例如,请注意样本输入后来如何显示为 "T\xe1\xbb\x89nh \xc4\x90\xe1\xbb\x93ng Nai"
.
如果这对你有用,我建议你创建一个一对一的映射,存储在字典中(及其反向),并在关键步骤简单地在两个方向上索引字典。我假设您可以自己处理检测逻辑(编码时为非 ASCII 字符;解码时为标记序列)。
我正在使用 xmlcharrefreplace
替换字符串中的非标准字符,以便可以将其保存在 xml 文件中。稍后,我想将此字符串重新转换回原始字符。
import openpyxl
import cgi
from html.parser import HTMLParser
parser = HTMLParser()
startingString = "Tỉnh Đồng Nai" #example string
print("Starting string: " + startingString) #Starting string: Tỉnh Đồng Nai
# 1. This string contains non-standard characters. Convert these characters using xmlcharrefreplace
escapedString = cgi.escape(startingString)
strEscapedString = str(escapedString)
aposString = strEscapedString.replace("'", "'")
savedToExcelString = str(aposString.encode('utf-8', 'xmlcharrefreplace') )[2:-1]
print("xmlcharrefreplace converted to: " + savedToExcelString) #xmlcharrefreplace converted to: T\xe1\xbb\x89nh \xc4\x90\xe1\xbb\x93ng Nai
# 2. The string is saved to an xml file
# 3. The string is read from an xml file
# 4. Convert the string back into the original starting string
unescapedString = parser.unescape(savedToExcelString)
#what do I do here??? I need to 'undo' the xmlcharrefreplace encoding
print(startingString + " == " + unescapedString + " is " + str(startingString == unescapedString))
# Tỉnh Đồng Nai == T\xe1\xbb\x89nh \xc4\x90\xe1\xbb\x93ng Nai is False
# ^^ Should be the same string at the end
请注意,我不能使用 codecs.open(),因为我正在使用库 openpyxl 打开一个包含数据的 Excel 文件。输入的字符集没有限制 - 我希望最终字符串与初始字符串相同。
目标:将字符从 xmlcharrefreplace 转换回其脚本字符。 例如:“\x90”变成“ồ”
您所描述的是不可能的,一般来说。当您从一个大字符集(任何)转换为一个较小的字符集时,您会丢失信息。除非您对编码结果有限制,否则您无法编码可以逆转的东西。
但是,如果您与数据库有一些约定,这些标签将不会出现在输入中(例如字符序列“\x”),您可以使用其中一个或多个表示编码字符串,您可以在该区域创建您喜欢的任何代码。例如,请注意样本输入后来如何显示为 "T\xe1\xbb\x89nh \xc4\x90\xe1\xbb\x93ng Nai"
.
如果这对你有用,我建议你创建一个一对一的映射,存储在字典中(及其反向),并在关键步骤简单地在两个方向上索引字典。我假设您可以自己处理检测逻辑(编码时为非 ASCII 字符;解码时为标记序列)。