简体   繁体   中英

Parsing a CSV row into columns with the first entry in each row as the first entry in each column

I have a csv file that I need to change so that I can create an input file. The data is set up so that it's keyname, and then data such as:

allendata,test@test.com,test1@test.com  
allendata2,test1@test.com,test@test.com,test3@test.com

I need the output formatted so that I end up with

allendata,test@test.com
allendata,test1@test.com
allendata2,test1@test.com
allendata2,test@test.com
allendata3,test3@test.com

There are about 1800 lines like this, so I'll end up with somewhere around 30000 lines when it's all parsed out.

I'll take any method possible, with bash or python being preferable.

Thanks, Allen

This should do the trick

fIn = open('testIn.txt', 'r')
fOut = open('testOut.txt','w')

for line in fIn:
    data = line.split(',')
    name = data[0]
    for address in data[1:]:
        fOut.write(name + ',' + address + '\n')

fIn.close()
fOut.close()

'textIn.txt'

allendata,test@test.com,test1@test.com  
allendata2,test1@test.com,test@test.com,test3@test.com

'testOut.txt'

allendata,test@test.com
allendata,test1@test.com    
allendata2,test1@test.com
allendata2,test@test.com
allendata2,test3@test.com

You could do it using awk like this:

$ awk -F, '{for(i=2;i<=NF;i++) print $1","$i}' file > new_file
$ cat new_file
allendata,test@test.com
allendata,test1@test.com  
allendata2,test1@test.com
allendata2,test@test.com
allendata2,test3@test.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM