【问题标题】:ascii file parsing based on position and length in line using python使用python基于位置和长度的ascii文件解析
【发布时间】:2012-03-16 20:00:41
【问题描述】:

我想将特定的 ascii 文件转换为 csv。 这个 ascii 文件有自己的规范,我在相关片段下面发布。
A 行以代码 66TP 开头:

66TP        1003    54.437269600149717.012388003107655.5139691177756                :10.008677993245250.01231534739191


B行以C6NM开头:

C6NM0821.565823793411260.900167346000671.2812114953994820.81007696688170033                                1679475490.0000000001679475527.0000000001


正如您在上面看到的,各个值没有分开,但它们的区别是 按行中的位置和长度。

A线规格:

1 Position   Length Data format Description of field

2   1           2   Type code   Record type code = 66
3   3           2   Derivation  Derivation code
4   5           16  Name        Point name
5   21          16  Latitude    Latitude
6   37          16  Longitude   Longitude
7   53          16  Distance    WGS84 ellipsoidal height at APC
8   69          16  Text 16     Feature code
9   85          1   GPS Method  Measurement method
10  86          1   Classification  Classification of the point
11  87          16  Distance    Horizontal precision
12  103         16  Distance    Vertical precision


B线规格:

1   Position Length Data format Description of field
2   1           2   Type code   Record type code = C6
3   3           2   Derivation  Derivation code
4   5           2   Integer 2   Minimum number of satellites
5   7           1   Boolean     Relative DOPs
6   8               16  Scalar  PDOP (maximum)
7   24          16  Scalar  HDOP (maximum)
8   40          16  Scalar  VDOP (maximum)
9   56          16  Scalar  RMS
10  72          4   Integer 4   Number of GPS positions used
11  76          16  Distance    Horizontal standard deviation
12  92          16  Distance    Vertical standard deviation
13  108         4   Integer 4   Start GPS week
14  112         16  Scalar  Start GPS time in seconds to 3dp
15  128         4   Integer 4   End GPS Week
16  132         16  Scalar  End GPS time in seconds to 3dp
17  148         1   Monitor Status


我想要的输出是合并两行,就像:

1003,54.4372696001497,17.0123880031076,55.5139691177756,0.009,0.012,8,1.6,0.9,1.28,20.8,033,1679,475490.0,1679,475527.0


这是输入文件,我在其中用方括号标记了各个值:

66TP        [1003]    [54.4372696001497][17.0123880031076][55.5139691177756]                :1[0.00867799324525][0.01231534739191]

C6NM[082][1.56582379341126][0.90016734600067][1.28121149539948][20.81007696688170][033]                                [1679][475490.000000000][1679][475527.0000000001]


抱歉发了很长的帖子,但我不知道如何用更短的方式描述它。 我是一个业余的初学者程序员,我想请你给我任何让我开始的提示 处理此类数据。

【问题讨论】:

  • CSV 输出是由您指定的吗? CSV 将做什么?
  • 它将被翻译成html表格作为gps调查报告。
  • 那为什么不直接输出HTML呢?
  • 因为在此期间我还必须对纬度、经度和高度进行计算,然后将所有内容放在桌面上。由于我不是熟练的程序员,因此检索计算所需的数据并将其转换为 html 对我来说是最简单的方法。
  • 那你根本不想输出文本;您只想将数据解析为一些内部数据结构,然后在输出 HTML 之前使用这些数据结构。接受的答案显示了如何做到这一点;你不会得到 csv 输出,而是每行的字符串 list

标签: python parsing csv ascii


【解决方案1】:

由于您知道每行中每个元素的位置,因此请使用string slice 来抓取每个元素。

例如,

type_code = linea[0:2]
(derivation, name) = (linea[2:4], linea[4:20])

为了更进一步,您可以编写一个小函数来根据线条的长度列表将线条分开。

代码

def split_string_by_position(a_string, lengths):
    result = []
    position = 0
    for length in lengths:
        result.append(a_string[position:position+length])
        position = position+length
    return result


line = '66TP        1003    54.437269600149717.012388003107655.5139691177756'
lengths = [2, 2, 16, 16, 16, 16, 16, 16, 1, 1, 16, 16]

print(split_string_by_position(line, lengths))

输出

['66', 'TP', '        1003    ', '54.4372696001497', '17.0123880031076', '55.5139691177756', '', '', '', '', '', '']

这只是返回数据元素的列表。您可以更进一步,通过提供变量名称以及每个长度 ([[2,'type'], [2,'derivation'],...]) 并稍微更改函数,使其返回 dict,然后您可以使用 the_result['variable_name'] 访问它

一些想法。 http://learnpythonthehardway.org/ 对您来说是一件好事,这样您就可以学习该语言的基础知识。

【讨论】:

  • 非常感谢,看起来很简单,但我还没有这么宽泛的视野,因为我与 python 的冒险只有一年之久。
  • 对,我根据您的问题假设。很高兴我能提供帮助,并且一定要注意 learnpythonthehardway 链接:)
【解决方案2】:

看起来这是用空格分隔的,在这种情况下,您可以忽略列号,只需使用 line.split() 来获取字段列表。

我的入桌计划也可能会有所帮助: http://stromberg.dnsalias.org/~strombrg/to-table.html

【讨论】:

    【解决方案3】:

    我认为使用生成器编写它会很有启发性(并且可能对某些人有用)。

    首先是代码:

      1 #!/usr/bin/env python
      2
      3 def parser(str,len):
      4     '''Generate parsed chunks from str based on a list of lengths, len'''
      5     position = 0
      6     for l in len:
      7         yield str[position:position+l]
      8         position = position + l
      9
     10 line = '66TP        1003    54.437269600149717.012388003107655.5139691177756'
     11 lengths = [2, 2, 16, 16, 16, 16, 16, 16, 1, 1, 16, 16]
     12
     13 lines = [ chunk for chunk in parser(line, lengths) ]
     14 print lines
    

    现在您可以在任何可以使用迭代器的地方使用解析器;比如我如何在第 13 行使用它来将所有字符串塞入一个名为 lines 的列表中。

    您还可以通过有趣的方式更改您的生成器,例如将 .strip() 添加到第 7 行的末尾。现在您的字段已经从每个字段的前面和后面删除了空格。

    按照描述修改第 7 行:

            yield str[position:position+l].strip()
    

    你现在得到了这个修改的输出:

    ['66', 'TP', '1003', '54.4372696001497', '17.0123880031076', '55.5139691177756', '', '', '', '', '', '']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-31
      • 2018-10-05
      • 1970-01-01
      • 2018-05-13
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      相关资源
      最近更新 更多