I am trying to load data with numpy.loadtxt... The file im trying to read is using cp1252 coding. Is there a possibility to change the encoding to cp1252 with numpy?

The following

import numpy as np
n = 10
myfile = '/path/to/myfile'
mydata = np.loadtxt(myfile, skiprows = n)

gives:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf6 in position 189: invalid start byte

The file contains metadata (first n rows) followed by a table of floats.

Edit: This problem only occurs when running this on Ubuntu (12.04). On Windows it works well. For this reason I think this problem is related to the encoding.

Edit2: opening the file as shown in the following works well, too:

import codecs
data = codecs.open(myfile, encoding='cp1252')
datalines = data.readlines()

However I'd like to use np.loadtext to directly read the data into a numpy array.

--------------------------------------------------------------------------------------

I could solve the problem by myself.

I just had to open the file with the appropriate before reading it with numpy:

import numpy as np
import codecs n=10 filecp = codecs.open(myfile, encoding = 'cp1252')
mydata = np.loadtxt(filecp, skiprows = n)

Thank you everyone!

 

关于loadtxt编码问题的解决方法的更多相关文章

  1. Java web应用中的常见字符编码问题的解决方法

    以下是 Java Web应用的常见编码问题 1. html页面的编码 在web应用中,通常浏览器会根据http header: Content-type的值来决定用什么encoding, 比如遇到Co ...

  2. [转载]Java web应用中的常见字符编码问题的解决方法

    以下是 Java web应用的常见编码问题 1. html页面的编码 在web应用中,通常浏览器会根据http header: Content-type的值来决定用什么encoding, 比如遇到Co ...

  3. sublime text 乱码生成.dump问题的解决方法

    title: sublime text 乱码生成.dump问题的解决方法 tags: sublime text,sublime text 3,.dump,乱码 grammar_cjkRuby: tru ...

  4. OWASP Top 10 – 2013, 最新十大安全隐患(ASP.NET解决方法)

    OWASP(开放Web软体安全项目- Open Web Application Security Project)是一个开放社群.非营利性组织,目前全球有130个分会近万名会员,其主要目标是研议协助解 ...

  5. ArcGis 属性表.dbf文件使用Excel打开中文乱码的解决方法

    2019年4月 拓展: ArcGis——好好的属性表,咋就乱码了呢? 2019年3月27日补充: 在ArcMap10.3+(根据官网描述应该是,作者测试使用10.5,可行)以后的版本,可以使用ArcT ...

  6. jsp中文乱码终极解决方法

    转载http://blog.csdn.net/csh624366188/article/details/6657350 一 找出问题的根源    乱码可能出现的地方:1 jsp页面中          ...

  7. html乱码原因与网页乱码解决方法

    造成html网页乱码原因主要是html源代码内中文字内容与html编码不同造成.但无论是哪种情况造成乱码在网页开始时候都需要设置网页编码. charset编码设置 html网页乱码效果截图 一.乱码造 ...

  8. Java中常见的几个乱码问题以及解决方法

    1.ajax--URL中的参数含中文,后台接受的参数出现乱码 解决方法: 第一步:在javascript中,对url进行两次编码 url = "http://localhost:8080/M ...

  9. Python 中文注释报错解决方法

    代码中一旦有了中文注释便会报错. 原因 如果文件里有非ASCII字符,需要在第一行或第二行指定编码声明. 解决方法 在第一行或是第二行加入这么一句# -- coding: utf-8 -- 完美解决

随机推荐

  1. redux & react-redux

    在vue中,可以使用vuex进行数据管理,在react中,可以使用redux进行数据管理.redux主要由Store.Reducer和Action组成: Store:状态载体,访问状态.提交状态.监听 ...

  2. OS&SYS&Shuti模块

    #sys.argv   主要针对脚本可以读取参数 Shuti模块 import shutil f1=open('笔记',encoding='utf-8') f2=open('笔记2','w',enco ...

  3. [UE4]瞬移

    1.设置Input,事件名称设置为Teleport 2.设置事件Teleport 3.

  4. DBus send byte array over gdbus ----Send dbus data

    遇到一个问题,如何通过dbus传送uint8数组元素 有3种方法, 1.直接传 ay 2.传  a(y) 3.xml定义为 ay,但是通过annotation 强行将 guchar 转为GVarian ...

  5. 用PS做圆角图片

    ps: Adobe Photoshop CS2  如果图片被锁定,请“双击”图层中“背景”解锁,如果没有图层菜单,在最上面导航栏中:窗口—图层.如下图:  点“确定”,解锁.  选用“圆角矩形工具”. ...

  6. bootstrap-treeview初使用

    <div id="tree">div> $(function () { function getTree() { var data = [{ text: &quo ...

  7. 【Linux命令】Linux下的tar压缩解压缩命令详解(转)

    tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个 ...

  8. mezzanine的page_menu tag(二)

    dict的特性,key可以是None >>> def f(): a=[2,3] return a #函数返回local变量 >>> a=f() >>&g ...

  9. linux下启动tomcat服务

     Linux下tomcat服务的启动.关闭与错误跟踪,使用PuTTy远程连接到服务器以后,通常通过以下几种方式启动关闭tomcat服务:切换到tomcat主目录下的bin目录(cd usr/local ...

  10. cmd创建文件命令

    一.建立空文件的几种方法 1.cd.>a.txt cd.表示改变当前目录为当前目录,即等于没改变:而且此命令不会有输出. >表示把命令输出写入到文件.后面跟着a.txt,就表示写入到a.t ...