---
title: "python处理非utf8编码文件转为utf8"
author: "xiaolin0199"
date: "2015-12-08T13:41:52Z"
tags: []
categories: ["编程语言"]
slug: "python-utf8-utf8"
draft: false
lastmod: "2015-12-08T13:43:12Z"
id: 2262568
url: "https://xiaolin0199.iteye.com/blog/2262568"
---

1 判断文件的编码

 

```
import chardet
def check_file_charset(file):
    with open(file,'rb') as f:
        return chardet.detect(f.read())    

    return {}
```

 2 编码转换

 

```
            import codecs
            from django.utils.encoding import smart_text

            f_type = check_file_charset(file_path)
            if f_type and 'encoding' in f_type.keys() and f_type['encoding'] != 'utf-8':
                try:
                    with codecs.open(file_path, 'rb', f_type['encoding']) as f:
                        content = smart_text(f.read())
                    with codecs.open(file_path, 'wb', 'utf-8') as f:
                        f.write(content)
                except:
                    pass
```