Python报错ValueError: invalid literal for int() with base 10: ‘\xef\xbb\xbf1′

出现场景:在打开UTF-8格式文件后,进行int类型转换时报错。

出错代码如下:

with open(file) as f:

for i in f:

print int(i.split(','))

报错如下:

ValueError: invalid literal for int() with base 10: '\xef\xbb\xbf1'

 
解决方法:

使用codecs.open 打开文件,它以UTF-8格式解码并忽略文件的初始BOM,最后返回Unicode的数据。

代码如下:

import codecs
 
with codecs.open(file, "r", "utf-8-sig") as f:
    for i in f:
        print int(i.split(','))

时间: 2024-08-03 05:50:33

Python报错ValueError: invalid literal for int() with base 10: ‘\xef\xbb\xbf1′的相关文章

Python ValueError: invalid literal for int() with base 10 实用解决方法

  这篇文章主要介绍了Python ValueError: invalid literal for int() with base 10 实用解决方法,本文使用了一个取巧方法解决了这个问题,需要的朋友可以参考下 今天在写爬虫程序的时候由于要翻页,做除法分页的时候出现了 代码如下: totalCount = '100' totalPage = int(totalCount)/20 ValueError: invalid literal for int() with base 10的错误 网上同样的

爬虫-python 2.7.6报错SyntaxError: invalid syntax

问题描述 python 2.7.6报错SyntaxError: invalid syntax 在学习爬虫的时候遇到了点问题: 'User-Agent':'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/34.0.1847.116 Chrome/34.0.1847.116 Safari/537.36' 这一行老是报错SyntaxError: invalid syntax hea

python 报错求解Undefined variable from import: urlopen

问题描述 python 报错求解Undefined variable from import: urlopen 解决方案 http://www.cnblogs.com/i-bugs/p/4028647.html 解决方案二: 第三方包引入时,eclipse默认会把一些包定为错误的,错误是: "undefined variable from import..." 其实是对的,可是报错,很烦人 解决方法: window -- preferences -- pydev -- editor -

python报错 wxpython运行出错

问题描述 python报错 wxpython运行出错 AttributeError: 'module' object has no attribute 'frame' 解决方案 我也不知道,我也出现过这个问题,求破 解决方案二: frame属性不存在..是不是引用了无效的属性,或者说代码版本和wxPython版本不一致导致的 解决方案三: 这个你就是得看看最新版本的代码是个什么属性了,查一下api看看 解决方案四: 你是不是没有正确安装上wxpython模块 解决方案五: 1.首先在shell试

用jep在java上运行python报错

问题描述 用jep在java上运行python报错 显示错误 ImportError: No module named site 代码是这样的 String SCRIPT_PATH = "D:\codes\Python\StockDataAnalysis\MatGraph.py"; try { Jep jep = new Jep(); jep.runScript(SCRIPT_PATH); } catch (Exception ex) { ex.printStackTrace(); }

Struts 标签 property=数组 报错:Invalid argument looking up property

问题描述 JSP代码:<logic:iterate id="questions" name="set" type="com.hs.actionForm.ExamForm" scope="request" indexId="ind"> ***************** <html:hidden property="idArr[${ind}]" name="qu

Python报错UnicodeDecodeError: ascii codec can t decode byte 0xe0 ...解决方法

Windows 8机器上安装Python2.7后,下载一些Package包进行setup时总是报错UnicodeDecodeError,如下: File "C:/Python27/lib/mimetypes.py", line 250, in enum_types ctype = ctype.encode(default_encoding) # omit in 3.x! UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 i

报错Syntax error on token &amp;quot;int&amp;quot;, Dimensions expected after this token

ArrayList<int> List = new ArrayList<int>(); 报错Syntax error on token "int", Dimensions expected after this token 原因:引用类型和原始类型没有搞清楚! Java提供两种不同的类型:引用类型和原始类型(或内置类型).Int是java的原始数据类型,Integer是java为int提供的封装类.Java为每个原始类型提供了封装类.   原始类型封装类 boo

typeerror-Python报错list indices must be integers, not str

问题描述 Python报错list indices must be integers, not str 分别是代码和报错,求大神们指点一二 解决方案 若是corterm是整数字符串,则把tfidf[corterm]改为tfidf[int(corterm)]即可. 解决方案二: corterm要为int类型,不能是字符串 解决方案三: 错误解释很清楚:你的列表的索引必须是int类型不能为str. 并且按照尝试,数组的下标一般都是整型的.