这是我的 CSV 文件的一个示例:
04/Feb/2016:06:38:44-0500,ab,3,10,57,200,10254
04/Feb/2016:06:39:07-0500,cd,1,42,168,304,0
04/Feb/2016:06:39:07-0500,ef,1,43,169,304,0
04/Feb/2016:06:39:07-0500,ab,1,43,170,304,0
04/Feb/2016:06:39:07-0500,cd,1,44,171,304,0
04/Feb/2016:06:39:07-0500,ef,1,45,172,304,0
我想获取第二列中的字符串,如果文件不存在,则创建名为该字符串的文件,并在文件中添加该特定行。因此,如下所示:
fetch string in 2nd column -> "ab" -> if file doesnt exist create file called "ab.csv" -> open file and add line "04/Feb/2016:06:38:44-0500,ab,3,10,57,200,10254"
fetch string in 2nd column -> "cd" -> if file doesnt exist create file called "cd.csv" -> open file and add line "04/Feb/2016:06:39:07-0500,cd,1,42,168,304,0"
fetch string in 2nd column -> "ef" -> if file doesnt exist create file called "ef.csv" -> open file and add line "04/Feb/2016:06:39:07-0500,ef,1,43,169,304,0"
fetch string in 2nd column -> "ab" -> if file doesnt exist create file called "ab.csv" -> open file and add line "04/Feb/2016:06:39:07-0500,ab,1,43,170,304,0"
fetch string in 2nd column -> "cd" -> if file doesnt exist create file called "cd.csv" -> open file and add line "04/Feb/2016:06:39:07-0500,cd,1,44,171,304,0"
fetch string in 2nd column -> "ef" -> if file doesnt exist create file called "ef.csv" -> open file and add line "04/Feb/2016:06:39:07-0500,ef,1,45,172,304,0"
结果:
ab.csv:
04/Feb/2016:06:38:44-0500,ab,3,10,57,200,10254
04/Feb/2016:06:39:07-0500,ab,1,43,170,304,0
----------------------------------------------
cd.csv:
04/Feb/2016:06:39:07-0500,cd,1,42,168,304,0
04/Feb/2016:06:39:07-0500,cd,1,44,171,304,0
----------------------------------------------
ef.csv:
04/Feb/2016:06:39:07-0500,ef,1,43,169,304,0
04/Feb/2016:06:39:07-0500,ef,1,45,172,304,0
感谢任何帮助!
答案1
使用awk
$ awk -F, '{print >> $2".csv"}' file.csv
$ cat ab.csv
04/Feb/2016:06:38:44-0500,ab,3,10,57,200,10254
04/Feb/2016:06:39:07-0500,ab,1,43,170,304,0
$ cat cd.csv
04/Feb/2016:06:39:07-0500,cd,1,42,168,304,0
04/Feb/2016:06:39:07-0500,cd,1,44,171,304,0
$ cat ef.csv
04/Feb/2016:06:39:07-0500,ef,1,43,169,304,0
04/Feb/2016:06:39:07-0500,ef,1,45,172,304,0
$
但请记住,真正的 CSV 文件可能包含带引号的逗号之内它们的逗号分隔字段 - 因此,在严肃使用时始终建议使用适当的 CSV 解析器:例如如何使用 Perl 读取 CSV 文件?或者PyMOTW:逗号分隔值文件。
答案2
我不知道您是否仍在寻找 Python 式的解决方案。steeldriver 的回答如此简单,让我感到惊讶,没想到它竟然awk
如此强大。
#!/usr/bin/env python
import csv
import os
def main():
with open("file.csv", "rb") as f:
reader = csv.reader(f)
for row in reader:
fname = row[1] + ".csv"
with open(fname, 'w') as f:
f.write(','.join([i for i in row]))
main()
不能责怪我为了闪亮的赏金而尝试:D