从字符串中提取一定范围内的unicode字符

2024-04-19 14:50:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个有很多垃圾字符的文本文件。你知道吗

https://raw.githubusercontent.com/shantanuo/marathi_spell_check/master/dicts/sample.txt

我只需要保留德夫纳加利字符。预期的干净输出将如下所示。。。你知道吗

भूमी
भूमी
भूमीला
भैय्यासाहेब
भैरवनाथ
भैरवी
भैरव
गावापासून
गा

根据这个页面,我需要提取所有字符之间的unicode范围的U+090到U+097 https://en.wikipedia.org/wiki/Devanagari_(Unicode_block)


我尝试了这个代码,但它返回了一些外来字符。你知道吗

def remove_junk(word):
    mylist=list()
    for i in word:
        if b'9' in (i.encode('ascii', 'backslashreplace')):
            mylist.append(i)
    return (''.join(mylist))

with open('sample2a.txt', 'w') as nf:
    with open('sample.txt') as f:
        for i in f:
            nf.write(remove_junk(i) + '\n')

Tags: sampleinhttpstxtforaswithopen
2条回答

我不知道Python,但我想可以像JavaScript一样在正则表达式中使用Unicode属性,因此可以通过使用天成文书脚本属性以某种方式调整以下脚本:

var text =
`‘भूमी
‘भूमी’
‘भूमी’ला
‘भैय्यासाहेब
‘भैरवनाथ
‘भैरवी
‘भैरव’
ﻇﻬﻴﺮ
(ページを閲覧しているビジターの使用言語)。
(缺少文字)
गावापासून
�गा`;
console.log (text.replace (/[^\r\n\p{Script=Devanagari}]/gu, ""));

由此产生:

भूमी
भूमी
भूमीला
भैय्यासाहेब
भैरवनाथ
भैरवी
भैरव



गावापासून
गा

可以使用regex删除unicode范围U+0900-U+097F之外的所有字符。你知道吗

import re

p = re.compile(r'[^\u0900-\u097F\n]')   # preserve the trailing newline
with open('sample.txt') as f, open('sample2a.txt', 'w') as nf:
    for line in f:
        cleaned = p.sub('', line)
        if cleaned.strip():
            nf.write(cleaned)

最小代码示例

import re

text = '''
‘भूमी
‘भूमी’
‘भूमी’ला
‘भैय्यासाहेब
‘भैरवनाथ
‘भैरवी
‘भैरव’
ﻇﻬﻴﺮ
(ページを閲覧しているビジターの使用言語)。
(缺少文字)
गावापासून
गा
'''

p = re.compile(r'[^\u0900-\u097F\n]')
for line in text.splitlines():
    cleaned = p.sub('', line)
    if cleaned.strip():
        print(cleaned)

# भूमी
# भूमी
# भूमीला
# भैय्यासाहेब
# भैरवनाथ
# भैरवी
# भैरव
# गावापासून 
# गा

相关问题 更多 >