给定一个在私有(private)使用区域包含字符的文件,如下所示:
$ cat textfile.txt | less
10 翴 30 <U+E4D1> ten-thirty in ... three ... two ... one .
- 10 翴 45だи<U+E145>砆 秂 <U+E18E> it 's a slam-dunk .
<U+E707> 10 翴 <U+E6C4>ㄓ ? so you will be home by 10:00 ?
10 翴 牧 よ<U+E6BC>ㄓ<U+E5EC> bogey at 10 o'clock .
- 10 翴 牧 よ<U+E6BC>い盠 - ten o'clock , lieutenant , 10 o'clock !
10 翴 牧 よ<U+E6BC>绰玭 i see it , 8 o'clock , heading south !
10 翴 筁<U+E5EC> it 's past 10:00 .
<U+E80B>ぱ 10 翴 非<U+E1A0>筁ㄓ be here tomorrow , 10:00 sharp .
- 10 , 老搭档 有 人 开枪 , 疑犯 拒捕 shots firing . suspect 's fleeing .
- 1 -0 而已 - only 1-0 .
- 1 -0 而已 - only 1-0 .
如果有任何字符超出 unicode 字节点,如何删除行?
我已经尝试过这个:
# ord(u'\uE000') == 57344
for line in open('test.txt'):
if any(ord(i) >57344 for i in line):
pass
else:
print (line)
但我似乎无法删除包含 PUA 字符的行。
如何在 unix bash 中使用 sed/awk 或其他东西而不是使用 Python 来实现相同的效果?
请注意,我仍然希望保留有效的 unicode 行,而不仅仅是保留带有 ascii 字符的行。例如。我想保留最后第三行的汉字和“……开枪了。嫌疑人逃跑了。” (由于某种原因,我无法在问题中输入中文部分,因为 SO 显示中文字符错误。)
最佳答案
检查字符是否属于 private use area 的标准 (ord(i) > 57344
)不正确的是:
Currently, three private use areas are defined: one in the Basic Multilingual Plane (
U+E000–U+F8FF
), and one each in, and nearly covering, planes 15 and 16 (U+F0000–U+FFFFD
,U+100000–U+10FFFD
)
这里是固定的 Python 3 代码:
pua_ranges = ( (0xE000, 0xF8FF), (0xF0000, 0xFFFFD), (0x100000, 0x10FFFD) )
def is_pua_codepoint(c):
return any(a <= c <= b for (a,b) in pua_ranges)
for line in open('test.txt', 'r'):
if any(is_pua_codepoint(ord(i)) for i in line):
pass
else:
print(line)
关于python - 如果一行包含专用区域字符,如何删除它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40501519/