简体   繁体   English

在Linux上使用PyQT4 / numpy进行Unicode可移植性

[英]Unicode portability with PyQT4/numpy on linux

I'm developing a multiplatform application in pyQT4 and numpy and actually it doesn't work on my linux system (Xubuntu 12.04) even so it seems work great on windows 7. So, the problem comes from my import files method (It is in a PyQT4 class) : 我正在pyQT4和numpy中开发一个多平台应用程序,实际上即使在Windows 7上它也无法正常运行,即使在Windows 7上也无法正常运行。因此,问题出在我的导入文件方法上( PyQT4类):

def import_folder(self,abs_path,folder_list):
    for folder_i in folder_list:
        filenames = glob.glob("%s/%s/*.txt" %( abs_path,folder_i ))
        aa =list(filenames)[1]
        print filenames
        data_fichier = np.genfromtxt("%s" %(aa),delimiter=';',skip_header=35,usecols=[8])
        data_fichier2 = np.zeros((data_fichier.shape[0],))

And this is the error I get : 这是我得到的错误:

     data_fichier = np.genfromtxt("%s" %aa,delimiter=';',skip_header=35,usecols=[8])
     File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 1241, in genfromtxt
     fhd = iter(np.lib._datasource.open(fname, 'rbU'))
     File "/usr/lib/python2.7/dist-packages/numpy/lib/_datasource.py", line 145, in open
      return ds.open(path, mode)
     File "/usr/lib/python2.7/dist-packages/numpy/lib/_datasource.py", line 472, in open
found = self._findfile(path)
     File "/usr/lib/python2.7/dist-packages/numpy/lib/_datasource.py", line 315, in _findfile
     filelist += self._possible_names(self.abspath(path))
     File "/usr/lib/python2.7/dist-packages/numpy/lib/_datasource.py", line 364, in abspath
     splitpath = path.split(self._destpath, 2)
     UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 14: ordinal not in range(128)

I have printed my "filenames" variables : 我已经打印了“文件名”变量:

   [u'/home/*****/Documents/Plate_test/Track-Arousal_experiment_24_06_13_track_1-Trial     2-7-Subject 1.txt', u'/home/*****/Documents/Plate_test/Track-Arousal_experiment_24_06_13_track_1-Trial     2-5-Subject 1.txt']

Therefore, the problem comes from the unicode mode (the "u" at the begining of the list elements). 因此,问题来自unicode模式(列表元素开头的“ u”)。 And I don't know at all why I get this unicode mode with my linux system. 而且我根本不知道为什么我的linux系统会获得这种unicode模式。 Have you any ideas how I can remove it and turn of in the "regular" mode (sorry for the terminology, I'm not an expert) ? 您是否知道如何删除它并以“常规”模式打开(对不起,我不是专家)? (Or others ideas about my problem). (或其他有关我的问题的想法)。

(Just for you know, when I have launched the method as a simple function without the PyQT class, it works great, so I suspect it.) (您知道吗,当我将方法作为简单函数启动而没有PyQT类时,它很好用,所以我怀疑它。)

Thanks, 谢谢,

Lem 莱姆

It seems that the loadtxt method expects a str and not a unicode since it tries to decode it. 似乎loadtxt方法期望使用str而不是unicode因为它尝试对其进行解码。 glob in your case is returning unicode strings so try encoding your filenames: 您的情况下的glob返回unicode字符串,因此请尝试对文件名进行编码:

filenames = map(str, filenames)

Or 要么

filenames = map(lambda f: f.encode('utf-8'), filenames)

Or even: 甚至:

def safe_str(s):
    if isinstance(s, unicode):
        return s.encode('utf-8')
    return s
filenames = map(safe_str, filenames)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM