在Python中,如何将列中的每个值拆分为单独的csv文件?

在Python中,如何将列中的每个值拆分为单独的csv文件?,python,csv,Python,Csv,我在csv文件中有这样一个列: ----- label ----- 0 1 0 2 1 4 3 0 import re test_f = open('test.csv') result_f = open('result.csv', 'a') for line in test_f: new_str = re.sub('[^a-zA-Z0-9\n\.]'," ", line) result_f.write(new_str) # also, this too, please: te

我在csv文件中有这样一个列:

-----
label
-----
0
1
0
2
1
4
3
0
import re
test_f = open('test.csv')
result_f = open('result.csv', 'a')
for line in test_f:
    new_str = re.sub('[^a-zA-Z0-9\n\.]'," ", line)
    result_f.write(new_str)

# also, this too, please:
test_f.close()
result_f.close()
现在,我需要将每个值写入不同的CSV文件,即:

0.csv
1.csv
...
7.csv
我尝试了以下代码,可以在单个csv文件中写入每一行:

-----
label
-----
0
1
0
2
1
4
3
0
import re
test_f = open('test.csv')
result_f = open('result.csv', 'a')
for line in test_f:
    new_str = re.sub('[^a-zA-Z0-9\n\.]'," ", line)
    result_f.write(new_str)

# also, this too, please:
test_f.close()
result_f.close()

但我不知道如何将每一行保存在不同的文件中。有更好的建议吗

这个代码怎么样?它已经使用上下文管理器(with)为您处理“关闭”方法,跳过标题并将内容拆分为文件,并拆分为具有不同关注点的函数!;)

下面是这个简单项目的文件夹结构。文件file_X.csv由上面的代码(Python 3.6.4)生成


抱歉,这是一个天真的问题。Python3.3与dst_filename=f'./csv/file_{str(index)}.csv'的等价物是什么?没问题,这将是dst_filename='./csv/file_{}.csv.格式(str(index)),我建议您更新Python安装,但现在,请查看并检查“新”格式;)非常感谢,@Danielder001随时,@Martin:)