简体   繁体   English

python f.read的奇怪回报

[英]strange return from python's f.read

I am trying to capture some data from a piece of hardware I'm developing through one of cypress' fx2lp chips. 我试图从我通过赛普拉斯的fx2lp芯片开发的一块硬件中捕获一些数据。 I used cypress' software to record a sample of my data stream to a file, which I am trying to read with python. 我使用cypress的软件将我的数据流样本记录到一个文件中,我正尝试使用python读取该文件。 However, when I read it, I'm getting some interesting output that I'm not sure how to interpret. 但是,当我阅读它时,会得到一些有趣的输出,不确定如何解释。

I am opening the file like this: 我正在这样打开文件:

f = open("testdata_5Aug2014.dat","rb")

Then I read the data in various sized chunks, similar to this: 然后,我读取各种大小的数据块,类似于以下内容:

f.read(100)

Typically, the result of the above line (and what I want to see) is something like this: 通常,上一行的结果(以及我想看到的结果)是这样的:

'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05\x12\x05\x12\x05\x12\x05\x12\x05\x12\x05\x12\x05\x12\x05\x12\x05\x12\x05\x12\x05\x12\x05\x12\x05\x12\x05\x12\x05\x12\x05\x12\x05\x12\x05\x12\x05\x12\x05\x12\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

But I sometimes get returns that include 't's and '?'s thrown in there like this: 但是有时我会得到这样的回报,其中包括“ t”和“?”:

'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00t\x14t\x14t\x14t\x14t\x14t\x14t\x14t\x14t\x14t\x14t\x14t\x14t\x14t\x14t\x14t\x14t\x14t\x14t\x14t\x14K\x01?\x00\xff??\x00\xff??\x00\xff??\x00\xff?\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

This is a problem, because when I use struct.unpack to parse this out, it won't return any of those bytes with the special characters appended. 这是一个问题,因为当我使用struct.unpack解析它时,它不会返回任何附加了特殊字符的字节。

So my question is: What are those symbols? 所以我的问题是:这些符号是什么? How did they get there? 他们如何到达那里? and How do I remove them or deal with them? 以及如何删除或处理它们?

You're reading binary data from a file, but f.read returns that data as a string. 您正在从文件中读取二进制数据,但是f.read以字符串f.read返回该数据。 When you print that string, it's interpreting those bytes as characters. 当您打印该字符串时,它会将那些字节解释为字符。 However, not every byte value maps to a displayable character, so some bytes are shown as escape sequences: \\x followed by two hexadecimal digits. 但是,并非每个字节值都映射到一个可显示的字符,因此某些字节显示为转义序列: \\x后跟两个十六进制数字。 For example, 0 shows up as \\x00 and 255 shows up as \\xff . 例如,0显示为\\x00和255显示为\\xff

Some values do map to characters, such as 63 mapping to '?' 某些值确实映射到字符,例如63映射到'?' and 116 mapping to 't'. 和116映射到“ t”。 The ord and chr functions can be used to fetch the numerical value of a character, and the character mapping for a number, respectively, so ord('t') returns 116 and chr(63) returns '?' ordchr函数可用于获取chr的数值,以及分别映射数字的字符,因此ord('t')返回116,而chr(63)返回'?' .

Either way, no matter how it's displayed, your data should be fine, and struct.unpack should be able to work with it as usual. 无论哪种方式,无论如何显示,您的数据都应该很好,并且struct.unpack应该能够照常使用它。

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

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