简体   繁体   English

如何将字节作为字节字符串而不是整数写入csv文件?

[英]How to write bytes to csv file as byte strings not integers?

There is a list of bytes objects (each one is 4 bytes) that is returned as output of one code and I want to save it into a .csv file using CSV module and read it back later in another script. 有一个字节对象列表(每个字节是4个字节),它们作为一个代码的输出返回,我想使用CSV模块将其保存到.csv文件中,并稍后在另一个脚本中读取。 Here is the code that I have learnt from python's official documentation: 这是我从python的官方文档中学到的代码:

import struct
import csv

k   = 0x100000
rng = range(0, k)
x1 = [b''] * k
x = 0xffffffff

for i in rng:
    x1[i]   = struct.pack("<L", x)
    x -= 1

print(x1[0])              # b'\xff\xff\xff\xff'

List = x1

with open("test.csv", 'w', newline='') as rF:
    wr = csv.writer(rF, dialect='excel')
    for i in List:
        wr.writerow(i)

When looking inside the created test.csv using notepad, instead of a column of byte strings I see 4 columns of 8-bit integers. 当使用记事本查看创建的test.csv ,而不是一列字节字符串,我看到了4列8位整数。 Few first lines of test.csv are: test.csv第一行几行是:

255,255,255,255
254,255,255,255
253,255,255,255
252,255,255,255
251,255,255,255
250,255,255,255
       .
       .
       .

What am I doing wrong that this is happening? 我正在做错什么事情了? Is there a way to get a csv file with one column of byte strings? 有没有一种方法来获取带有一列字节字符串的csv文件? for example: 例如:

b'\xff\xff\xff\xff'
b'\xfe\xff\xff\xff'
b'\xfd\xff\xff\xff'
          .
          .
          .

Actually I do not care how are my bytes stored in a csv. 其实我不在乎我的字节如何存储在csv中。 I just care to have them back into a list of bytes using csv.reader in another script and want the loading process be the quickest possible. 我只是想在另一个脚本中使用csv.reader将它们放回到字节列表中,并希望加载过程尽可能快。

This will do. 这样就可以了。

import pandas as pd
import struct

k   = 0x100000
rng = range(0, k)
x1 = [b''] * k
x = 0xffffffff

for i in rng:
    x1[i]   = struct.pack("<L", x)
    x -= 1

df = pd.DataFrame()
df["data"] = x1
df.to_csv("test.csv", index=False, header=None)

This will output file in bytes. 这将以字节为单位输出文件。 Sample output 样品输出

b'\xff\xff\xff\xff'
b'\xfe\xff\xff\xff'
b'\xfd\xff\xff\xff'
b'\xfc\xff\xff\xff'
b'\xfb\xff\xff\xff'

You can use pandas instead of csv, to read the file back. 您可以使用pandas而不是csv来回读文件。

df = pd.read_csv("test.csv")

Alternative 替代

with open("test.csv", "wb") as f:
    for i in x1:
        f.write(i)
        f.write('\n'.encode('utf-8'))

# Reading file
y = []
with open("test.csv", "rb") as f:
    for i in f.readlines():
        y.append(i.replace('\n'.encode('utf-8'), "".encode("utf-8")))
pprint(y[:10])

Output 产量

[b'\xff\xff\xff\xff',
 b'\xfe\xff\xff\xff',
 b'\xfd\xff\xff\xff',
 b'\xfc\xff\xff\xff',
 b'\xfb\xff\xff\xff',
 b'\xfa\xff\xff\xff',
 b'\xf9\xff\xff\xff',
 b'\xf8\xff\xff\xff',
 b'\xf7\xff\xff\xff',
 b'\xf6\xff\xff\xff']

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

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