在Python中检查有效的utf8字符串

在Python中检查有效的utf8字符串

本文介绍了在Python中检查有效的utf8字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从文件系统读取文件名,我想将它们作为JSON编码数组发送.问题是文件系统上的文件可以用无效的编码存储,我需要处理这种情况以在将无效的文件名传递给json.dump之前忽略无效的文件名,否则它将失败.

I'm reading filenames from file system and I want to send them as JSON encoded array. The problem is that files on file system can be stored in invalid encoding, and I need to handle this situation to omit invalid filenames before passing it to json.dump, otherwise it will fail.

有没有办法检查我的字符串(文件名)是否包含有效的utf-8字符?

Is there a way to check that my string (filename) contains valid utf-8 chars?

推荐答案

尝试以下操作如何?

valid_utf8 = True
try:
    filename.decode('utf-8')
except UnicodeDecodeError:
    valid_utf8 = False

...基于此处类似问题的答案:

... based on an answer to a similar question here: How to write a check in python to see if file is valid UTF-8?

这篇关于在Python中检查有效的utf8字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:38