Tenge Asked:2020-04-05 18:04:44 +0000 UTC2020-04-05 18:04:44 +0000 UTC 2020-04-05 18:04:44 +0000 UTC 如何将编码更改为特殊字符 772 有一个带有编码的文本文件 test.txt: ! € ✖ ы и тд 如何将这些编码转换为相应的字符:!€ ✖ s等 我手动尝试使用命令sed -i -e 's/ы/ы/g' test.txtEverything works,但有超过一千个这样的字符。这项工作需要一天多的时间。 замена 2 个回答 Voted Best Answer gil9red 2020-04-05T18:27:24Z2020-04-05T18:27:24Z 通过python3,这可以在几行中完成。 算法: 使用一种编码,让它成为utf-8 打开文件进行读取 ( f_in) 打开另一个文件进行写入 ( f_out)。也可以在同一个文件里做记录,不过我觉得还是新建一个比较好,以免以后做备份 html替换是通过内置库完成的: https ://docs.python.org/3/library/html.html#html.unescape 例子: import html with open('test.txt', encoding='utf-8') as f_in, \ open('new_test.txt', 'w', encoding='utf-8') as f_out: text = f_in.read() f_out.write(html.unescape(text)) 测试.txt: ! Hello € Мир ✖ ! ы 新测试.txt: ! Hello € Мир ✖ ! ы user236980 2020-04-05T18:35:06Z2020-04-05T18:35:06Z 有Java一个用于处理文本的特殊库: <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-text</artifactId> <version>1.8</version> </dependency> 使用示例: public static void main(String[] args) throws Exception { soutUnescaped("!"); soutUnescaped("€"); soutUnescaped("€"); soutUnescaped("ы"); // ! // € // € // ы } private static void soutUnescaped(String escapedString) { System.out.println(StringEscapeUtils.unescapeHtml4(escapedString)); }
通过python3,这可以在几行中完成。
算法:
utf-8f_in)f_out)。也可以在同一个文件里做记录,不过我觉得还是新建一个比较好,以免以后做备份html替换是通过内置库完成的: https ://docs.python.org/3/library/html.html#html.unescape例子:
测试.txt:
新测试.txt:
有
Java一个用于处理文本的特殊库:使用示例: