Python basics tutorial:
Flask supports file uploading. Recently, we made a function to upload SQL files, in which Chinese, Thai, Malaysian and other languages are used. We received the files through Flask, and saved the files in the back end first, but found that the files were garbled after saving.
Receiving and saving Chinese files through the above code would be garbled.
How to solve it?
First of all, let’s think about how garbled code is created, two different ways of encoding it. The detect method of Python’s Chardet module can be used to get the encoding of the string
The encoding method obtained in the project is GB2312 encoding. To solve the problem of incorrect encoding, it is to decode correctly first and then re-encode. In this project, GB2312 decoding is first used, and then UTF-8 encoding is used. As follows:
Sql_content is re-written to the file object and then saved to solve the problem of Chinese garbled characters.
After the problem of garbled files saved between backgrounds was solved, the garbled code uploaded by MINIO’s SDK was also solved. Code overview:
More basic Python tutorials will continue to be updated! Or partners have what content need to learn can also leave a message!