简体   繁体   English

将CSV行解析为各列,并将每行中的第一个条目作为每列中的第一个条目

[英]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.

相关问题 pandas为每个DatetimeIndex条目获取第一个过滤行的有效方法 - pandas efficient way to get first filtered row for each DatetimeIndex entry 读取每列的第一个元素,然后读取 csv 文件中的整行 - Reading first element of each column and then the entire row in csv file 如何将每一行保存到 dataframe 中的 csv 并根据每一行的第一列命名文件 - How to save each row to csv in dataframe AND name the file based on the the first column in each row CSV文件Python第一栏中每个条目的前置日期 - Pre-Pending Date To Each Entry In First Column of a CSV File Python Python:通过另一列中的每个唯一行条目对数据框中的多列求和 - Python: Sum multiple columns in a Data Frame by each unique row entry in another column 如何将列中的列表/数组条目转换为一行,每个条目具有不同的列 - how to convert lists/array entries in a column to one row with different columns for each entry 获取每列的第一个非零条目并记录相应的索引值 - fetch the first nonzero entry for each column and record the corresponding index value PySpark - 获取第一列的每一行 - PySpark - Getting each row of first column 访问列表Python中每一行的第一列 - Access the first column in each row in a list Python 选择列中每行的前N个元素 - select the first N elements of each row in a column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM