小编Jb_*_*Eyd的帖子

Python:将包含非ASCII字符的列表写入文本文件

我正在使用python 3.4,我正在尝试将一个名称列表写入文本文件.清单如下:

my_list = ['Dejan Živkovi?','Gregg Berhalter','James Stevens','Mike Windischmann',
               'Gunnar Heiðar Þorvaldsson']
Run Code Online (Sandbox Code Playgroud)

我使用以下代码导出列表:

file = open("/Users/.../Desktop/Name_Python.txt", "w")
file.writelines( "%s\n" % item for item in my_list )
file.close()
Run Code Online (Sandbox Code Playgroud)

但它不起作用.Python似乎不喜欢非ASCII字符,并给我以下错误:

"UnicodeEncodeError: 'ascii' codec can't encode character '\u017d' in position 6: ordinal not in range(128)"
Run Code Online (Sandbox Code Playgroud)

你知道有没有办法解决这个问题?也许可以用UTF-8/unicode写文件?

python ascii newline file list

7
推荐指数
1
解决办法
7154
查看次数

在Python上启动HDF5库时权限被拒绝

我正在使用Unix,并且需要使用HDF5来存储数据。根据HDF5的指南http://docs.h5py.org/en/latest/quick.html#quick,应首先创建一个新文件。

import h5py
import numpy as np
f = h5py.File("mytestfile.hdf5", "w")
Run Code Online (Sandbox Code Playgroud)

但是,一旦运行此代码,我就会收到一个奇怪的错误。

IOError: Unable to create file (Unable to open file: name = 'mytestfile.hdf5', errno = 13, 
error message = 'permission denied', flags = 13, o_flags = 602)
Run Code Online (Sandbox Code Playgroud)

我不知道错误的含义。有人知道我是否需要特定权限才能使用此库?提前致谢!

python hdf5

5
推荐指数
0
解决办法
3211
查看次数

按组分配的 R 分位数

我有以下 df:

group = rep(seq(1,3),30)
variable = runif(90, 5.0, 7.5)
df = data.frame(group,variable)
Run Code Online (Sandbox Code Playgroud)

我需要 i) 按组定义分位数,ii) 将每个人分配给与她的组相关的分位数。

因此,输出将如下所示:

id    group  variable  quantile_with_respect_to_the_group
1      1      6.430002     1
2      2      6.198008     3
          .......
Run Code Online (Sandbox Code Playgroud)

有一种复杂的方法可以在每个组上使用循环和剪切功能,但它根本没有效率。有人知道更好的解决方案吗?

谢谢 !

r quantile tapply

3
推荐指数
1
解决办法
5493
查看次数

字典中最常见的值

我有以下字典:

d = {"a":["MRS","VAL"],"b":"PRS","c":"MRS","d":"NTS"}
Run Code Online (Sandbox Code Playgroud)

我想创建一个字典,给出每个值的出现.基本上,它看起来像:

output = {"MRS":2,"PRS":1,"NTS":1,"VAL":1}
Run Code Online (Sandbox Code Playgroud)

有谁知道我怎么能这样做?提前致谢 !

python sorting dictionary

1
推荐指数
1
解决办法
1522
查看次数

标签 统计

python ×3

ascii ×1

dictionary ×1

file ×1

hdf5 ×1

list ×1

newline ×1

quantile ×1

r ×1

sorting ×1

tapply ×1