zoukankan      html  css  js  c++  java
  • Bug的解决

    1. 关于使用Pandas的read_csv读取CSV文件。
      用了三台Mac,在有header,同时有英文字符和中文字符的情况下,使用诸如datafile.loc[0:1 'column的名称']的方式无法正确读取出csv文件里面的column的名称,总是报错(在某一台mac生成的文件是可以正常读取)
      报错的内容:datafile.title和datafile.index显示出来都是正常的;但是要么出现KeyError:(1,'column的名称')的错误,要么是the label [column的名称] is not in the [columns]
      尝试了各种另存为,用写字板改变编码,xls转csv等等都不行,中途还有utf-16的文件不能解析等错误。用sep=',', head=None之类的都不成功。
      问题之所在:还是编码问题。在mac下面将文件直接用Excel另存为中选择文件格式为制表符分割的文本(.txt)之后,还是用read_csv就可以正常读取了,不论文件中是否有中文字符,可以自由选择某一列(column)里面的内容了。

    ps:可以import chardet来查看文件的编码方式
    Pandas还可以实现SQL方式,比如datafile.groupby('column的名称').size()

    1. 解决matplotlib中文乱码问题(Mac下)
      首先下载一个ttc格式的字体(比如songti),window下为ttf格式。然后放在matplotlib里面的font文件夹里(可能换个地方也行)

    import matplotlib
    matplotlib.use('qt4agg')
    from matplotlib.font_manager import *
    import matplotlib.pyplot as plt
    myfont = FontProperties(fname='/路径/songti.ttc')

    用以下代码测试即可

    plt.plot([-1,2,-5,3])
    plt.title(u'中文',fontproperties=myfont)
    plt.show()

  • 相关阅读:
    【leetcode】Valid Palindrome
    【leetcode】Longest Common Prefix
    【leetcode】Intersection of Two Linked Lists
    【leetcode】Compare Version Numbers
    【leetcode】Pascal's Triangle
    ubuntu 14.04下spark简易安装
    【leetcode】Number of 1 Bits
    【leetcode】LRU Cache
    【leetcode】Container With Most Water
    【leetcode】Roman to Integer
  • 原文地址:https://www.cnblogs.com/hankoui/p/8586974.html
Copyright © 2011-2022 走看看