在python中读取Cobol(.cbl)文件,并从中提取注释行

问题描述 投票:0回答:1

我有一个.cbl文件,其中有很多行。我想阅读并将注释行形式提取到.txt文件中。

例如:一种。 000200 *程序ID。 AP540P00。(要么)b。 *程序ID。 AP540P00。我需要在第7位检查*。提取所有注释行后,将其打印到文本文件。

我这样做:将open('AP540P00.cbl','r')设为f:对于f中的行:s =设定(线)如果(s中的'*'):打印(行)

但是我只需要在每行的第7个索引处专门检查* ...

python-3.x comments cobol
1个回答
0
投票

第7列中的“ *”或“ /”或该行上任何位置的“ *>”。写入文本文件。Python 3.7

代码:

line = ''
fout = open('z:comments.txt', 'wt')
x = open('z:code.txt', 'rt')
for line in x:
    if len(line) > 6 and (line[6] == '*' or line[6] == '/' \
                or line.find('*>') > -1):
            fout.write(line)
fout.close()

输入:

000200* PROGRAM-ID. AP540P00.
      * PROGRAM-ID. AP540P00.
       code
       code 
      * comment 1

       code 
       code *> in-line comment 
      * comment 2
       code 
       code 
      / comment 3
       code 
       code 
      * comment 4
       code 
       code 

输出:

000200* PROGRAM-ID. AP540P00.
      * PROGRAM-ID. AP540P00.
      * comment 1
       code *> in-line comment 
      * comment 2
      / comment 3
      * comment 4
© www.soinside.com 2019 - 2024. All rights reserved.