Why Nostr? What is Njump?
2023-05-05 11:36:14
in reply to

阿白 on Nostr: You 定义一个函数,检查字符串中是否包含中文字符 def ...

You
定义一个函数,检查字符串中是否包含中文字符
def contains_chinese(s): for ch in s: if u’\u4e00’ <= ch <= u’\u9fff’: return True return False 这个判断中文的函数有缺陷,会把日文字符判断为中文,如何优化

AI
要优化这个函数以排除日文字符,您可以使用unicodedata库来区分中文和日文字符。这是一个修改过的函数:

import unicodedata

def contains_chinese(s):
for ch in s:
if u’\u4e00’ <= ch <= u’\u9fff’ and ‘CJK UNIFIED IDEOGRAPH’ in unicodedata.name(ch):
return True
return False
这个函数仍然会检查字符是否在中文字符范围内,但它还会使用unicodedata.name()来确保字符是中文字符(即名称包含“CJK UNIFIED IDEOGRAPH”)。这样,如果字符是日文字符,它就不会被误判为中文字符。

我先按这个算法用已经筛出的id(1200+)测试一下
Author Public Key
npub1ccjq3jwtlhwv358r0g6xglxhc5zg9f9sex63xxl62ut6fl5efu4sysmr09