从ASCII文件中提取数据 [英] Extract data from ASCII file

查看:165
本文介绍了从ASCII文件中提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含几行类似的文件:


:10000000E7280530AC00A530AD00AD0B0528AC0BE2


我要提取的数据是8进制字符串,第一个是

,这是E728,像这样:


:10000000 E728 0530 AC00 A530 AD00 AD0B 0528 AC0B E2

此外,字符串中的字节相反。 E728必须是28E7,

0530需要3005等等。


我可以用C ++和Pascal做到这一点,但它看起来像Python可能更适合这项任务。


如何使用Python实现这一目标?

Suppose I have a file containing several lines similar to this:

:10000000E7280530AC00A530AD00AD0B0528AC0BE2

The data I want to extract are 8 hexadecimal strings, the first of
which is E728, like this:

:10000000 E728 0530 AC00 A530 AD00 AD0B 0528 AC0B E2

Also, the bytes in the string are reversed. The E728 needs to be 28E7,
0530 needs to be 3005 and so on.

I can do this in C++ and Pascal, but it seems like Python may be more
suited for the task.

How is this accomplished using Python?

推荐答案

使用Python 2.3:
With Python 2.3:
def splitter(line):
.. .. line = line [9:] #skip prefix

.... while line:

.... prefix,line = line [:4],line [4:]

....产生前缀[2:] +前缀[:2]

....分配器中的数字('':10000000E7280530AC00A530AD00AD0B0528AC0BE2' '):
....打印号码

....

28E7

3005

00AC

30A5

00AD

0BAD

2805

0BAC

E2
def splitter( line ): .... line = line[9:] # skip prefix
.... while line:
.... prefix, line = line[:4],line[4:]
.... yield prefix[2:]+prefix[:2]
.... for number in splitter( '':10000000E7280530AC00A530AD00AD0B0528AC0BE2''): .... print number
....
28E7
3005
00AC
30A5
00AD
0BAD
2805
0BAC
E2



如果要转换十六进制字符串到实际整数,使用

int(前缀,16)。


HTH,

Mike

Ren写道:

假设我有一个文件包含与此类似的几行:

:10000000E7280530AC00A530AD00AD0B0528AC0BE2

我想要的数据提取是8个十六进制字符串,第一个是E728,它是这样的:

:10000000 E728 0530 AC00 A530 AD00 AD0B 0528 AC0B E2

另外,字符串中的字节是相反的。 E728需要28E7,0530需要3005等等。

我可以用C ++和Pascal做到这一点,但似乎Python可能更适合任务。

如何使用Python实现这一目标?


If you want to convert the hexadecimal strings to actual integers, use
int( prefix, 16 ).

HTH,
Mike

Ren wrote:
Suppose I have a file containing several lines similar to this:

:10000000E7280530AC00A530AD00AD0B0528AC0BE2

The data I want to extract are 8 hexadecimal strings, the first of
which is E728, like this:

:10000000 E728 0530 AC00 A530 AD00 AD0B 0528 AC0B E2

Also, the bytes in the string are reversed. The E728 needs to be 28E7,
0530 needs to be 3005 and so on.

I can do this in C++ and Pascal, but it seems like Python may be more
suited for the task.

How is this accomplished using Python?



_______________________________________

Mike C. Fletcher

Designer,VR Plumber,Coder
http://成员.rogers.com / mcfletch /


_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/


Ren写道:
Ren wrote:
假设我有一个文件包含与此类似的几行:

:10000000E7280530AC00A530AD00AD0B0528AC0BE2
Suppose I have a file containing several lines similar to this:

:10000000E7280530AC00A530AD00AD0B0528AC0BE2




假设该文件名为data.txt

试试这个:

---------------------------------

def process(line):

line = line [9:]

result = []

for i in range(0,32 ,4):

result.append(行[i + 2:i + 4] +行[i:i + 2])

返回结果


for line in line(" data.txt"):

打印流程(行)

- -------------------------------

对于您的单个示例数据行,它会打印

['''28E7'',''3005'',''00AC'',''30A5'',''00AD'',''0BAD'',''2805'',' '0BAC'']


这是一个包含8个提取的十六进制字符串的列表。

而不是打印列表你可以用它做任何你想做的事情。

如果您需要更多信息,请询问。


- Irmen de Jong



Say the file is called data.txt
Try this:
---------------------------------
def process(line):
line=line[9:]
result=[]
for i in range(0,32,4):
result.append( line[i+2:i+4] + line[i:i+2] )
return result

for line in open("data.txt"):
print process(line)
---------------------------------
For your single example data line, it prints
[''28E7'', ''3005'', ''00AC'', ''30A5'', ''00AD'', ''0BAD'', ''2805'', ''0BAC'']

It''s a list containing the 8 extracted hexadecimal strings.
Instead of printing the list you can do whatever you want with it.
If you need more info, just ask.

--Irmen de Jong


Ren,

如果你去这里:

http://www.python.org/doc/current/tu...00000000000000


大约一半它谈到字符串切片的页面。


wes


Ren写道:
Ren,
If you go here:

http://www.python.org/doc/current/tu...00000000000000

about half way down the page it talks about string slicing.

wes

Ren wrote:
假设我有一个包含与此类似的几行的文件:

:10000000E7280530AC00A530AD00AD0B0528AC0BE2
我要提取的数据是8个十六进制字符串,第一个
这是E728,如下所示:

:10000000 E728 0530 AC00 A530 AD00 AD0B 0528 AC0B E2

此外,字符串中的字节也相反。 E728需要28E7,0530需要3005等等。

我可以用C ++和Pascal做到这一点,但似乎Python可能更适合用于完成任务。

如何使用Python实现这一目标?
Suppose I have a file containing several lines similar to this:

:10000000E7280530AC00A530AD00AD0B0528AC0BE2

The data I want to extract are 8 hexadecimal strings, the first of
which is E728, like this:

:10000000 E728 0530 AC00 A530 AD00 AD0B 0528 AC0B E2

Also, the bytes in the string are reversed. The E728 needs to be 28E7,
0530 needs to be 3005 and so on.

I can do this in C++ and Pascal, but it seems like Python may be more
suited for the task.

How is this accomplished using Python?






这篇关于从ASCII文件中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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