Python读取格式化字符串
我有一个包含多行的文件,其格式如下语法:
FIELD POSITION DATA TYPE
------------------------------
COOP ID 1-6 Character
LATITUDE 8-15 Real
LONGITUDE 17-25 Real
ELEVATION 27-32 Real
STATE 34-35 Character
NAME 37-66 Character
COMPONENT1 68-73 Character
COMPONENT2 75-80 Character
COMPONENT3 82-87 Character
UTC OFFSET 89-90 Integer
数据全部采用 ASCII 格式。
一行的一个例子是:
011084 31.0581 -87.0547 26.0 AL BREWTON 3 SSE ------ ------ ------ +6
我当前的想法是,我想一次读取一行文件,并且不知何故将每一行分解成一个字典,以便我可以引用组件。是否有一些模块可以在 Python 中执行此操作,或者以其他干净的方式执行此操作?
谢谢!
I have a file with a number of lines formatted with the following syntax:
FIELD POSITION DATA TYPE
------------------------------
COOP ID 1-6 Character
LATITUDE 8-15 Real
LONGITUDE 17-25 Real
ELEVATION 27-32 Real
STATE 34-35 Character
NAME 37-66 Character
COMPONENT1 68-73 Character
COMPONENT2 75-80 Character
COMPONENT3 82-87 Character
UTC OFFSET 89-90 Integer
The data is all ASCII-formatted.
An example of a line is:
011084 31.0581 -87.0547 26.0 AL BREWTON 3 SSE ------ ------ ------ +6
My current thought is that I'd like to read the file in a line at a time and somehow have each line broken up into a dictionary so I can refer to the components. Is there some module that does this in Python, or some other clean way?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:您仍然可以使用结构模块:
请参阅结构模块文档。在我看来,您想使用 struct.unpack()
您想要的可能是这样的:
EDIT: You can still use the struct module:
See the struct module documentation. Looks to me like you want to use
struct.unpack()
What you want is probably something like:
我想我从您的问题/评论中了解您在寻找什么。如果我们假设 Real、Character 和 Integer 是唯一的数据类型,那么下面的代码应该可以工作。 (我还将假设您显示的格式文件是制表符分隔的):
您最终应该得到包含字典列表的结果,其中每个字典都是从格式文件中的键名称到正确数据类型的数据值的映射。
I think I understand from your question/comments what you are looking for. If we assume that Real, Character, and Integer are the only data types, then the following code should work. (I will also assume that the format file you showed is tab delimited):
You should end up with results containing a list of dictionaries where each dictionary is a mapping from key names in the format file to data values in the correct data type.
看起来你可以相当简单地使用字符串和切片编写一个函数。 string[0:5] 将是第一个元素。它是否需要可扩展,或者可能是一次性的?
It seems like you could write a function using strings and slices fairly simply. string[0:5] would be the first element. Does it need to be extensible, or is it likely a one off?