仅使用“for”循环转换文本记录

仅使用“for”循环转换文本记录

我有一个文本输入文件,由如下所示的行组成:

352|C|PAID|7036|VOICE|01-FEB-12

我需要将每一行分成|(竖线)字符处的字段,并使用标签输出每个字段;即,产生以下形式的行:

{ "IMSI":"352", "Status":"C", "ServiceType":"PAID", "Number":"7036", "ConnectionType":"VOICE", "ActivationDate":"01-FEB-12" }

我怎样才能做到这一点?

答案1

要完全在 shell 中完成此操作:

while IFS="|" read -r im st se nu co ac
do
    printf '{ "%s":"%s", "%s":"%s", "%s":"%s", "%s":"%s", "%s":"%s", "%s":"%s" }\n' \
        IMSI           "$im" \
        Status         "$st" \
        ServiceType    "$se" \
        Number         "$nu" \
        ConnectionType "$co" \
        ActivationData "$ac"
done < input > output

答案2

使用一点 Python 进行更改:

#!/usr/bin/env python

import csv
import json
import sys
from collections import OrderedDict

keys = ['IMSI', 'Status', 'ServiceType', 'Number', 'ConnectionType', 'ActivationData']

with open(sys.argv[1], 'rt') as csvfile:
    reader = csv.reader(csvfile, delimiter='|')
    for row in reader:
        print json.dumps(OrderedDict(zip(keys, row)))

输出:

{"IMSI": "352", "Status": "C", "ServiceType": "PAID", "Number": "7036", "ConnectionType": "VOICE", "ActivationData": "01-FEB-12"}

相关内容