Reputation: 3
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,[email protected],[email protected]
allendata2,[email protected],[email protected],[email protected]
I need the output formatted so that I end up with
allendata,[email protected]
allendata,[email protected]
allendata2,[email protected]
allendata2,[email protected]
allendata3,[email protected]
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
Upvotes: 0
Views: 141
Reputation: 117876
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,[email protected],[email protected]
allendata2,[email protected],[email protected],[email protected]
'testOut.txt'
allendata,[email protected]
allendata,[email protected]
allendata2,[email protected]
allendata2,[email protected]
allendata2,[email protected]
Upvotes: 1
Reputation: 3646
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,[email protected]
allendata,[email protected]
allendata2,[email protected]
allendata2,[email protected]
allendata2,[email protected]
Upvotes: 1