Home > Backend Development > Python Tutorial > 用python实现的去除win下文本文件头部BOM的代码

用python实现的去除win下文本文件头部BOM的代码

WBOY
Release: 2016-06-16 08:46:44
Original
1013 people have browsed it

问题:
windows环境下新建或编辑文本文件,保存时会在头部加上BOM。
使用ftp上传到linux下,在执行时第一行即报错。
以下方法可以去除BOM头,有需要的朋友可以参考下。

复制代码 代码如下:

import codecs
data = open("Test.txt").read()
if data[:3] == codecs.BOM_UTF8:
 data = data[3:]
print data.decode("utf-8")

说明: 文件开始部为 0xEF 0xBB 0xBF 为BOM

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template