关于loadtxt编码问题的解决方法
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编码问题的解决方法的更多相关文章
- Java web应用中的常见字符编码问题的解决方法
以下是 Java Web应用的常见编码问题 1. html页面的编码 在web应用中,通常浏览器会根据http header: Content-type的值来决定用什么encoding, 比如遇到Co ...
- [转载]Java web应用中的常见字符编码问题的解决方法
以下是 Java web应用的常见编码问题 1. html页面的编码 在web应用中,通常浏览器会根据http header: Content-type的值来决定用什么encoding, 比如遇到Co ...
- sublime text 乱码生成.dump问题的解决方法
title: sublime text 乱码生成.dump问题的解决方法 tags: sublime text,sublime text 3,.dump,乱码 grammar_cjkRuby: tru ...
- OWASP Top 10 – 2013, 最新十大安全隐患(ASP.NET解决方法)
OWASP(开放Web软体安全项目- Open Web Application Security Project)是一个开放社群.非营利性组织,目前全球有130个分会近万名会员,其主要目标是研议协助解 ...
- ArcGis 属性表.dbf文件使用Excel打开中文乱码的解决方法
2019年4月 拓展: ArcGis——好好的属性表,咋就乱码了呢? 2019年3月27日补充: 在ArcMap10.3+(根据官网描述应该是,作者测试使用10.5,可行)以后的版本,可以使用ArcT ...
- jsp中文乱码终极解决方法
转载http://blog.csdn.net/csh624366188/article/details/6657350 一 找出问题的根源 乱码可能出现的地方:1 jsp页面中 ...
- html乱码原因与网页乱码解决方法
造成html网页乱码原因主要是html源代码内中文字内容与html编码不同造成.但无论是哪种情况造成乱码在网页开始时候都需要设置网页编码. charset编码设置 html网页乱码效果截图 一.乱码造 ...
- Java中常见的几个乱码问题以及解决方法
1.ajax--URL中的参数含中文,后台接受的参数出现乱码 解决方法: 第一步:在javascript中,对url进行两次编码 url = "http://localhost:8080/M ...
- Python 中文注释报错解决方法
代码中一旦有了中文注释便会报错. 原因 如果文件里有非ASCII字符,需要在第一行或第二行指定编码声明. 解决方法 在第一行或是第二行加入这么一句# -- coding: utf-8 -- 完美解决
随机推荐
- SpringSecurity-UsernamePasswordAuthenticationFilter的作用
UsernamePasswordAuthenticationFilter应该是我们最关注的Filter,因为它实现了我们最常用的基于用户名和密码的认证逻辑. 先看一下一个常用的form-login配置 ...
- 图的遍历——DFS(矩形空间)
首先,这里的图不是指的我们一般所说的图结构,而是大小为M*N的矩形区域(也可以看成是一个矩阵).而关于矩形区域的遍历问题经常出现,如“寻找矩阵中的路径”.“找到矩形区域的某个特殊点”等等之类的题目,在 ...
- Kubernetes Kubelet安全认证连接Apiserver
Kubelet使用安全认证连接Apiserver,可以用Token或证书连接.配置步骤如下. 1,生成Token命令 head -c /dev/urandom | od -An -t x | tr - ...
- [UE4]瞬移
1.设置Input,事件名称设置为Teleport 2.设置事件Teleport 3.
- 性能监控扩展篇(grafana + influxdb + telegraf)
之前已经说过了自己写sh脚本监控,我看有人评论了说用telegraf进行数据收集,于是乎去研究了下,感觉还可以,不过磁盘io的的表个人感觉有些美中不足,并未直接给出读写速率的情况,可能是研究时间太短, ...
- 解决Tomcat启动时项目重复加载问题
前几天一个同学项目要上线,部署到服务器时,因为客户需要通过IP直接可以访问到,所以在server.xml做了如下的配置 导致启动tomcat时候出现一个项目重复加载了两次,很容易就出现了内存溢出. 这 ...
- 自己动手实现JDK动态代理
出自:作者:孤独烟 http://rjzheng.cnblogs.com/ ------------------------------------------------------------- ...
- Xeon Phi 《协处理器高性能编程指南》随书代码整理 part 1
▶ 第三章,逐步优化了一个二维卷积计算的过程 ● 基准代码 #include <stdio.h> #include <stdlib.h> #include <string ...
- Python第2天
今天学习的主要内容: pycharm专业版的安装和注册,采用注册码的方式注册. 运算符,+ — * / // % < > <= >= != <> . 基本数据类型 ...
- 【java】String类常见操作
秋招做题需要,总结String类常用api如下: 简单的:str.length().str.isEmpty().str.split(“;”)切割 1.字符串反转:借助StringBuilder/Str ...