关于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 -- 完美解决
随机推荐
- 【剑指offer】数组中只出现一次的数字
题目:一个整型数组里除了两个数字之外,其他的数字都出现了偶数次.请写程序找出这两个只出现一次的数字. 思路1:使用HashMap存上所有的数字,数字作为Key,Value为对应的出现次数.这种做法可以 ...
- hash 在 perl 中的用法(转载)
Perl的数据结构中最有趣的一个特性是哈希(hash),它使得在数据片段之间建立键-值(key-value)关联成为可能.虽然这些哈希要远远比普通系统中以数字索引的数组用途更广,但是往往也会使初学者不 ...
- MySQL之Haproxy+Keepalived+MySQL高可用均衡负载部署 (网络摘抄)
来源于:https://blog.csdn.net/weisong530624687/article/details/71536837?utm_source=blogxgwz3 一.安装主从MySQL ...
- scrapy-pipeline的方法
scrapy中多个pipeline作用: 一个项目可能需要爬取多个网站,根据每个网站的数据量(处理方式)不同,可创建多个管道 pipeline class SpideranythingPipeline ...
- FTP管理常用命令
#新增用户liuhui,指定群组为groupa,附加群组为groupb,家目录为/ftp/groupbuseradd -g groupa -G groupb -d /ftp/groupb linhui ...
- LeetCode OJ 117. Populating Next Right Pointers in Each Node II
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- 记号一次更换IBM X3650M4主板后RAID无法启动的解决
https://wenku.baidu.com/view/9d503ef367ec102de2bd89d7.html 强烈感谢上面分享文档的大侠!! 1.更换主板后,linux系统,无法加载引导.需要 ...
- Linux的nmon监控结果分析文件中网络分析NET
1.首先,使用# ifconfig查看Linux系统中的网卡名称,有的是eth0,有的是em1,以查看结果为准,下图为em1 2.先试试Linux系统中有没有安装ethtool工具,没有的话,下载et ...
- MySQL慢查询日志相关的配置和使用。
MySQL慢查询日志提供了超过指定时间阈值的查询信息,为性能优化提供了主要的参考依据,是一个非常实用的功能,MySQL慢查询日志的开启和配置非常简单,可以指定记录的文件(或者表),超过的时间阈值等就可 ...
- python学习Day1 计算机原理编程思维
一.学习思想:3W+1H 学什么(what).为什么学(why).用在哪里(where).怎么用(how) 学习编程语言重在代码量.代码量.代码量! 二.计算机五大组成部分,三大核心: 五大组成部 ...