![](/img/trans.png)
[英]Read data from columns in .xlsx and save each one as seperate .txt file with python
[英]Read csv lines and save it as seperate txt file, named as a line - python
我有一些简单的代码问题。 我有一个带有一栏和几百行的csv文件。 我想获得一个代码来读取csv的每一行并将其另存为单独的txt文件。 重要的是,txt文件应已命名为读取行。
例如:1.亚当2.多罗蒂3.巴勃罗
会给我adam.txt,doroty.txt和pablo txt。 文件。 请帮忙。
这应该在python 3.6上做您需要的
with open('file.csv') as f: # Open file with hundreds of rows
for name in f.read().split('\n'): # Get list of all names
with open(f'{name.strip()}.txt', 'w') as s: # Create file per name
pass
另外,您可以使用内置的CSV库来避免解析csv文件带来的任何麻烦:
import csv
with open('names.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
file_name ='{0}.txt'.format(row['first_name'])
with open(file_name, 'w') as f:
pass
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.