为Python输出文件命名创建for循环
我正在导入一个名字的列表,比如:
文本文件里会包含:
Eleen
Josh
Robert
Nastaran
Miles
my_list = ['Eleen','Josh','Robert','Nastaran','Miles']
然后我把每个名字都放到一个列表里,我想为这个列表里的每个名字写一个新的Excel文件。
#1. Is there anyway I can create a for loop where on the line:
temp = os.path.join(dir,'...'.xls')
_________________________
def high_throughput(names):
import os
import re
# Reading file
in_file=open(names,'r')
dir,file=os.path.split(names)
temp = os.path.join(dir,'***this is where i want to put a for loop
for each name in the input list of names***.xls')
out_file=open(temp,'w')
data = []
for line in in_file:
data.append(line)
in_file.close()
3 个回答
试试这个:
in_file=open(names,'r')
dir,file=os.path.split(names)
for name in in_file:
temp = os.path.join(dir, name + '.xls')
with open(temp,'w') as out_file:
# write data to out_file
看看这个openpyxl
,特别是如果你需要创建.xlsx
文件的话。下面的例子假设你创建的Excel工作簿是空白的。
from openpyxl import Workbook
names = ['Eleen','Josh','Robert','Nastaran','Miles']
for name in names:
wb = Workbook()
wb.save('{0}.xlsx'.format(name))
我还是不太明白你想要做什么(说“不太明白”其实是“完全搞不懂”),不过我可以告诉你一些你做错的地方,以及怎么做才对:
in_file=open(names,'r')
dir,file=os.path.split(names)
temp = os.path.join(dir,'***this is where i want to put a for loop
for each name in the input list of names***.xls')
现在,你还没有输入名字的列表。这些名字是从 in_file
里读取的,但你还没有读取。之后,你会把这些名字读到 data
里,这样你就可以使用它们了。所以:
in_file=open(names,'r')
dir,file=os.path.split(names)
data = []
for line in in_file:
data.append(line)
in_file.close()
for name in data:
temp = os.path.join(dir, '{}.xls'.format(name))
out_file=open(temp,'w')
注意我把 for 循环放在了函数调用之外,因为你必须这样做。这是好事,因为你显然是想在这个循环里打开每个路径(并对每个文件进行操作),而不是打开一个由文件循环构成的单一路径。
但是如果你不坚持使用 for 循环,还有一种可能更接近你想要的方式:列表推导式。你有一个名字的列表。你可以用这个列表来构建一个路径的列表。然后你可以用这个路径列表来打开文件。像这样:
paths = [os.path.join(dir, '{}.xls'.format(name)) for name in data]
out_files = [open(path, 'w') for path in paths]
然后,等你构建好想要写入所有文件的字符串后,你可以这样做:
for out_file in out_files:
out_file.write(stuff)
不过,这种设计有点奇怪。主要是因为你必须关闭每个文件。它们可能会被垃圾回收自动关闭,即使没有被关闭,它们也可能会被刷新……但是,除非你运气好,否则你写入的所有数据都只是静静地待在内存的缓冲区里,根本没有写入磁盘。通常你不想写依赖运气的程序。所以,你需要关闭你的文件。按照这种设计,你得做点像这样的事情:
for out_file in out_files:
out_file.close()
其实回到我一开始建议的那个大循环会简单很多,这样你可以这样做:
for name in data:
temp = os.path.join(dir, '{}.xls'.format(name))
out_file=open(temp,'w')
out_file.write(stuff)
out_file.close()
或者,更好的是:
for name in data:
temp = os.path.join(dir, '{}.xls'.format(name))
with open(temp,'w') as out_file:
out_file.write(stuff)
在这里再说几句……
首先,你真的不应该试图手动从字符串生成 .xls 文件。你可以使用像 openpyxl
这样的库。或者你可以创建 .csv 文件——用 Python 内置的 csv
库很容易创建,Excel 也能轻松处理它们,就像处理 .xls 文件一样。或者你可以使用 win32com
或 pywinauto
来控制 Excel,让它为你创建文件。真的,任何方法都比手动生成要好。
其次,能写 for line in in_file:
说明 in_file
是某种行的序列。所以,如果你只想把它转换成一个行的 list
,你可以一步完成:
data = list(in_file)
但实际上,你想要这个列表的唯一原因是为了后面能循环它,创建输出文件,对吧?那为什么不直接等着,先循环文件里的行呢?
无论你怎么生成输出内容,先做这个。然后再用文件名列表循环,写入内容。像这样:
stuff = # whatever you were doing later, in the code you haven't shown
dir = os.path.dirname(names)
with open(names, 'r') as in_file:
for line in in_file:
temp = os.path.join(dir, '{}.xls'.format(line))
with open(temp, 'w') as out_file:
out_file.write(stuff)
这替代了你示例中的所有代码(除了那个名为 high_throughput
的函数,它导入了一些模块然后什么也没做)。