json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig): line 1 column 1
问题描述:使用Python代码将txt城市列表文件转换为xls文件,源码如下,
#!/usr/bin/env Python
# coding=utf-8
import os
import json
import xlwt # 存放文件的目录
filepath = '/home/tarena/python/20180312' def run():
os.chdir(filepath)
# 读取文件内容
with open('city.txt') as f:
content = f.read()
# 转为json
d = json.loads(content)
file = xlwt.Workbook()
# 添加sheet
table = file.add_sheet('test')
for row, i in enumerate(list(d)):
table.write(row, 0, i)
table.write(row, 1, d[i])
file.save('city.xls') if __name__ == "__main__":
run()
报错误:json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig): line 1 column 1 (char 0)错误,
分析原因是因为txt文件包含BOM字符,去掉BOM字符,在content = f.read()代码下加上:
if content.startswith(u'\ufeff'):
content = content.encode('utf8')[3:].decode('utf8')
转载于 https://blog.csdn.net/liu_xzhen/article/details/79563782
json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig): line 1 column 1的更多相关文章
- python:json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes问题解决
有如下一个文件,内容如下 { "test1": "/root/test/test1.template", "test2": "/r ...
- python中报错"json.decoder.JSONDecodeError: Expecting value:"的解决
在学习python语言中用json库解析网络数据时,我遇到了两个编译错误:json.decoder.JSONDecodeError: Expecting property name enclosed ...
- json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
1.json模块常用的四个函数 import json json.load() # 将一个存储在文件中的json对象(str)转化为相对应的python对象 json.loads() # 将一个jso ...
- python Json报错json.decoder.JSONDecodeError
近期工作中遇到一个问题,执行json.loads(json_data)时,在json_data中加上tab空格后就报错,不加则不报错 一.json.loads(json_data) 报错json.de ...
- json.loads 报错 json.decoder.JSONDecodeError
json.loads报json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes 出现这个错误其实只 ...
- json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (ch
阐述 想要把一个字符串转化成字典对象,在使用json的过程出现如此报错 解决方法 将字符串里面的单引号改为双引号
- python,json解析字符串时ValueError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
今天写测试工具的时候,去excel取数据,用json解析字符串为字典时报错,后经调试,发现是单引号的原因,将单引号换位双引号即可 def getExcelValue_to_dic(filepath): ...
- json.Decoder vs json.Unmarshal
128down voteaccepted It really depends on what your input is. If you look at the implementation of t ...
- SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data错误的解决
记录个报错: 问题描述: 经过服务器生成图片返到前台时,在火狐浏览器中下载图片或打开图片时报错:SyntaxError: JSON.parse: unexpected character at lin ...
随机推荐
- 【F12】chrome浏览器中 F12 功能的简单介绍
chrome浏览器中 F12 功能的简单介绍 由于F12是前端开发人员的利器,所以我自己也在不断摸索中,查看一些博客和资料后,自己总结了一下来帮助自己理解和记忆,也希望能帮到有需要的小伙伴,嘿嘿! 首 ...
- [转]Angular CLI 安装和使用
本文转自:https://www.jianshu.com/p/327d88284abb 一. 背景介绍: 两个概念: 关于Angular版本,Angular官方已经统一命名Angular 1.x统称为 ...
- SQL 查看表每一个列的名字以及类型
select a.name,b.name from sys.columns as a join sys.types as b on a.system_type_id=b.system_type_id ...
- 不要使用 JWT 进行会话管理
英文原文地址:Stop using JWT for sessions 最近我发现越来越多的人推荐使用 JWT 来在 Web 应用中管理会话(Session),这是一个非常非常糟糕的主意,在这篇文章中我 ...
- Hadoop专有名词
Hadoop专有名词 一. HDFS 二. MapReduce 1.MRAppMaster:MapReduce Application Master 负责整个过程调度和协调的 2.MapTask:在M ...
- Cylinder Candy(积分+体积+表面积+旋转体)
Cylinder Candy Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Edward the confectioner is ...
- power of the test
https://www.youtube.com/watch?v=UApFKiK4Hi8
- Linux常用基本命令:tr-替换或者删除字符
tr命令 作用:从标准输入中替换,缩减或者删除字符,并将结果输出到标准输出 格式:tr [option] [set1] [set2] tr [选项] [字符1] [字符2] 把y替换m, o替换e,并 ...
- 列表中文字太多 溢出使用省略号css方法
我们经常会遇到文字太多,而为了不打破原有布局,需要将多出文字用省略号代替,实现以下效果: 文字太太太太多多多啦...... 这个不多. html:这是个列表.ul/ol都行. <ul> & ...
- linux下安装apache环境
Centos6.5 64位下安装apache php mysql(安装包版本一定要正确,网上很多都不准,不要用32位的安装包装再64位上,后期有很多问题,下载安装后一定要检出版本,有的网友操蛋,把3 ...