gpt4 book ai didi

python - 需要 RE 来检测 UTF-8

转载 作者:太空宇宙 更新时间:2023-11-03 18:58:14 25 4
gpt4 key购买 nike

我有以下代码

inf = codecs.open(inPath , encoding='utf-8')
outf = codecs.open(outPath, encoding='utf-8', mode='w')
old = u'’;'
new = u'’&#59;'
for line in inf:
line = line.replace(old,new)
asc = line.encode('ascii', 'xmlcharrefreplace')
outf.write(asc)
# print asc
inf.close()
outf.close()

这(正确地)使用数字格式将智能引号和重音字符等转换为其 html 实体格式。它将转换

<p>Dreams like: “Someday I’ll travel to…; someday I’ll write a book;

进入

<p>Dreams like: &#8220;Someday I&#8217;ll travel to&#8230;; someday I&#8217;ll write a book; 

这都是正确的。

然而,代码进一步下游,看到中间的 …;,删除双分号,然后提示它没有有效的实体。我无法更改此代码。

正如您从我的代码中看到的,我发现了一种实体后跟分号的情况。我不想替换源代码中的所有分号。

如何检测代码点 > 127 的 UTF-8 字符后面的分号,以便将其替换为 ;?谢谢。

最佳答案

脸掌!

如果我先转换为 htmlentites,然后将 ;; 替换为 ;E 就可以解决我的问题。

self 提醒 - 考虑你在哪里做事,以及做什么!

关于python - 需要 RE 来检测 UTF-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16760665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com