[英]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. 我有一个需要更改的csv文件,以便可以创建输入文件。 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. 这样大约有1800行,所以当全部解析完后,我将最终得到大约30000行。
I'll take any method possible, with bash or python being preferable. 我将采取任何可能的方法,最好使用bash或python。
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' 'textIn.txt'
allendata,test@test.com,test1@test.com
allendata2,test1@test.com,test@test.com,test3@test.com
'testOut.txt' '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
来做到这一点:
$ 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.