关于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 -- 完美解决
随机推荐
- 动态规划经典——最长公共子序列问题 (LCS)和最长公共子串问题
一.最长公共子序列问题(LCS问题) 给定两个字符串A和B,长度分别为m和n,要求找出它们最长的公共子序列,并返回其长度.例如: A = "HelloWorld" B = & ...
- C++ Programming Language中的narrow_cast实现
在C++中,各种数值类型的转化是C++编译过程中警告的主要来源,但是,很多时候,我们需要使用各种数值类型,例如我们用数组的某一位表示大小为对应序号的值,这种情况下,经常会涉及多种数值类型.根据C++ ...
- [UE4]Retainer Box
把子元素的内容渲染到一个Render Target上去,然后放把它放置到到屏幕上去. Retainer Box的作用: 1.控制UI更新频率 2.把渲染后的UI当成Texture,放入材质中,加工后, ...
- 知识点:Java 集合框架图
知识点:Java 集合框架图 总结:Java 集合进阶精讲1 总结:Java 集合进阶精讲2-ArrayList Java集合框架图 我们经常使用的Arrayist.LinkedList继承的关系挺复 ...
- Using a ScrollView - RN4
使用滚动条. 1. import import {ScrollView} from "react-native"; 2. Using <ScrollView> ... ...
- leetcode14:最长公共字符串
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...
- mybatis xml中返回map 参看aiwanpai
<!-- 指定日期活动被创建次数查询结果数据集--> <resultMap id="countPlayTimesMap" type="HashMap&q ...
- threading 多线程使用
实例 1import threading #线程import time def Say(n): print('Test %d' %n) time.sleep(2) if __name__ == '__ ...
- jQuery.extend 与 jQuery.fn.extend
extend方法为jQuery对象的核心之一,语法如下: jQuery.extend([deep], target, object1, [objectN]),返回值Object. 概述:用一个或多个其 ...
- cdnbest的proxy里api用法案例:
用户的proxy帐号里api key要设置好,那个key设置后是不显示的,但会显示已设置 key是自已随便生成的 $uid = 22222; $skey = 'langansafe&*#'; ...