Python读取格式化的字符串

Python读取格式化的字符串

本文介绍了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

数据全部为ASCII格式.

The data is all ASCII-formatted.

一行示例是:

011084  31.0581  -87.0547   26.0 AL BREWTON 3 SSE                  ------ ------ ------ +6

我目前的想法是,我想一次一行地读取文件,并且以某种方式将每一行分解成一个字典,以便我可以引用这些组件.是否有某些模块可以在Python中执行此操作,或者采用其他一些干净的方法?

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?

谢谢!

推荐答案

编辑:您仍然可以使用struct模块:

EDIT: You can still use the struct module:

请参见结构模块文档.在我看来,您想使用 struct.unpack()

See the struct module documentation. Looks to me like you want to use struct.unpack()

您想要的可能类似于:

import struct
with open("filename.txt", "r") as f:
    for line in f:
        (coop_id, lat, lon, elev, state, name, c1, c2, c3, utc_offset
         ) = struct.unpack("6sx8sx9sx6sx2sx30sx6sx6sx6sx2s", line.strip())
        (lat, lon, elev) = map(float, (lat, lon, elev))
        utc_offset = int(utc_offset)

这篇关于Python读取格式化的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 08:01