一個簡單文本處理問題的多種解法

問題:python

將以下形式文本shell

Name: John Doe1
address : somewhere
phone: 123-123-1234

Name: John Doe2
address : somewhere
phone: 123-123-1233

Name: John Doe3
address : somewhere
phone: 123-123-1232

 

轉換成以下形式spa

Name: John Doe1                address : somewhere          phone: 123-123-1234
Name: John Doe2                address : somewhere          phone: 123-123-1233
Name: John Doe3                address : somewhere          phone: 123-123-1232

 

 

解法1:awkcode

awk 'BEGIN { FS="\n"; RS=""; OFS="\t\t" } { print $1, $2, $3 }' file.txt

解法2:sedip

cat input.txt | sed '/^$/d' | sed 'N; s:\n:\t\t:; N; s:\n:\t\t:' 
cat input.txt | sed '/^$/d' | sed 'N; N; s:\n:\t\t:g' 

解法3:pythoninput

#!/usr/bin/env python

def parse(inputfile, outputfile):
    dictInfo = {'Name':None, 'address':None, 'phone':None}
    for line in inputfile:
    if line.startswith('Name'):
        dictInfo['Name'] = line.split(':')[1].strip()
    elif line.startswith('address'):
        dictInfo['address'] = line.split(':')[1].strip()
    elif line.startswith('phone'):
        dictInfo['phone'] = line.split(':')[1].strip()
        s = 'Name: '+dictInfo['Name']+'\t'+'address: '+dictInfo['address'] \
            +'\t'+'phone: '+dictInfo['phone']+'\n'
        outputfile.write(s)

if __name__ == '__main__':
    with open('output.txt', 'w') as outputfile:
    with open('infomation.txt') as inputfile:
        parse(inputfile, outputfile)

 解法4 (paste)it

chenqi@chenqi-OptiPlex-760:~/mypro/python/file-parsing$ paste -s -d'\t\t\t\n' infomation.txt 
Name: John Doe1	address : somewhere	phone: 123-123-1234	
Name: John Doe2	address : somewhere	phone: 123-123-1233	
Name: John Doe3	address : somewhere	phone: 123-123-1232
相關文章
相關標籤/搜索