Numpy recarray将字节文字标签写入我的csv文件? [英] Numpy recarray writes byte literals tags to my csv file?

查看:471
本文介绍了Numpy recarray将字节文字标签写入我的csv文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下测试码

import numpy as np
import csv

data = np.zeros((3,),dtype=("S24,int,float"))
with open("testtest.csv", 'w', newline='') as f:
    writer = csv.writer(f,delimiter=',')
    for row in data:
        writer.writerow(row)

csv文件中的数据对于记录数组的字符串组件有b''标签(字节文字标签)。
什么是正确的方式来处理写csv这些记录数组和避免在我的csv文件中的字节文字标签的最好的方法?

And the data in the csv file has b'' tags (byte literal tags) for the string components of the record array. What is the proper way to handle writing to csv of these record arrays and the best way to avoid having byte literal tags in my csv file?

推荐答案

我想你正在使用Python3,它使用unicode作为默认字符串类型。字节字符串然后得到特殊的 b 标记。

I think you are working with Python3 which uses unicode as the default string type. byte strings then get special b marking.

如果我用unicode而不是字节生成数据,

If I generate the data with unicode instead of bytes, this works:

In [654]: data1 = np.zeros((3,),dtype=("U24,int,float"))
In [655]: data1['f0']='xxx'  # more interesting string field
In [656]: with open('test.csv','w') as f:
    writer=csv.writer(f,delimiter=',')
    for row in data1:
        writer.writerow(row)
In [658]: cat test.csv
xxx,0,0.0
xxx,0,0.0
xxx,0,0.0

np.savetxt 也做同样的事情:

In [668]: np.savetxt('test.csv',data1,fmt='%s',delimiter=',')
In [669]: cat test.csv
xxx,0,0.0
xxx,0,0.0
xxx,0,0.0

问题是,我可以解决这个问题,同时保留 S24 字段吗?例如,打开文件 wb

The question is, can I work around this while keeping the S24 field? For example by opening the file as wb?

我先前在 http://stackoverflow.com/a中探讨过这个问题/ 27513196/901925
尝试从我的Numpy数组中除去b''

看起来我的解决方案是 decode 字节字段,或直接写入字节文件。由于你的数组有字符串和数字字段的混合, decode 解决方案有点更繁琐。

Looks like my solutions are to either decode the byte field, or to write to a byte file directly. Since your array has a mix of string and numeric fields, the decode solution is a bit more tedious.

data1 = data.astype('U24,i,f') # convert bytestring field to unicode






辅助函数可用于解码字节字符串:

In [147]: fn = lambda row: [j.decode() if isinstance(j,bytes) else j for j in row]
In [148]: with open('test.csv','w') as f:
    writer=csv.writer(f,delimiter=',')
    for row in data:
        writer.writerow(fn(row))
   .....:         
In [149]: cat test.csv
xxx,0,0.0
yyy,0,0.0
zzz,0,0.0

这篇关于Numpy recarray将字节文字标签写入我的csv文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆