Check if utf8 string is correct in Python

I am reading the file names from the file system and I want to send them as a JSON encoded array. The problem is that the files in the file system may be stored in an invalid encoding, and I need to handle this situation to omit the invalid file names before transferring them to json.dump , otherwise it will fail.

Is there a way to verify that my string (file name) contains valid utf-8 characters?

+8
json python utf-8
source share
1 answer

How to try the following?

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

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

+17
source share

All Articles