小编Sha*_*shi的帖子

使用python识别垃圾unicode字符串

我的脚本是从csv文件中读取数据,csv文件可以有多个英语或非英语单词串.

有时候文本文件有垃圾字符串,我想识别那些字符串并跳过这些字符串并处理其他字符串

doc = codecs.open(input_text_file, "rb",'utf_8_sig')
fob = csv.DictReader(doc)
for row, entry in enumerate(f):
    if is_valid_unicode_str(row['Name']):
         process_futher

def is_valid_unicode_str(value):
     try:
         function
         return True
     except UnicodeEncodeError:
         return false
Run Code Online (Sandbox Code Playgroud)

csv输入:

"Name"
"袋è¢âdcx€¹Ã¤Â¸Å½Ã¦Å“‹å‹们çâ€ÂµÃ¥Â­Âå•â€"
"??????"
"John Dove"
Run Code Online (Sandbox Code Playgroud)

我想玷污函数is_valid_unicode_str(),它将识别垃圾字符串并仅处理有效字符串.

我尝试使用解码但是在解码垃圾字符串时它并没有失败

value.decode('utf8')
Run Code Online (Sandbox Code Playgroud)

预期的输出是带有中文和英文字符串的字符串

你能指导我如何实现过滤有效的Unicode文件的功能?

python mojibake python-2.7 python-unicode

3
推荐指数
1
解决办法
1559
查看次数

python re.search(正则表达式)仅搜索具有{{world}}模式的单词

我有在我所喜欢插入自定义标记HTML文件{{name}},{{surname}}.现在,我想寻找谁喜欢的模式完全匹配的标签{{world}}唯一甚至不是{world}},{{world},{world},{ word },{{ world }},等我写的小代码为

re.findall(r'\{(\w.+?)\}', html_string)
Run Code Online (Sandbox Code Playgroud)

它返回我不想要的模式{{world}},{world},{world}}之后的单词.我想完全匹配{{world}}.有人可以指导我吗?

python regex matching

1
推荐指数
1
解决办法
250
查看次数

读取文件后生成事件

我想在完成文件读取之后在Java中引发一些事件,以便处理程序可以捕获事件并继续执行下一个任务.我无法在完成文件读取后找到Java中存在的事件.

是他们的任何方式,或任何其他替代方案.

java file-handling readfile r.java-file

1
推荐指数
1
解决办法
397
查看次数

在Python中__threshold__有什么用?

我正在浏览一些Python自动化脚本,我看到了新的(对我来说)关键字 ___threshold__ = 0.6.它表明了什么?它用于什么?

python

0
推荐指数
1
解决办法
102
查看次数