创建用于命名输出文件Python的for循环

创建用于命名输出文件Python的for循环,python,excel,csv,for-loop,Python,Excel,Csv,For Loop,所以我要导入一个名字列表 e、 g 文本文件将包括: 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,'.

所以我要导入一个名字列表 e、 g

文本文件将包括:

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()
看一下,尤其是当您需要创建
.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)

for name in in_file:
    temp = os.path.join(dir, name + '.xls')
    with open(temp,'w') as out_file:
        # write data to out_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')
此时,您没有名称的输入列表。这就是您在_文件中读取的内容,但您尚未阅读。稍后,您将这些名称读入
数据
,然后可以使用它们。因此:

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]
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)
然后,在建立要写入所有文件的字符串后,可以执行以下操作:

for out_file in out_files:
   out_file.write(stuff)
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 out_file in out_files:
   out_file.close()
回到我最初建议的一个大循环可能要简单得多,所以您可以这样做:

for out_file in out_files:
   out_file.write(stuff)
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并使其创建您的文件。真的,任何事情都比试图手工生成它们要好

其次,您可以为inu文件中的行编写
这一事实意味着inu文件中的
是某种行序列。因此,如果您只想将其转换为一个
行列表
,您可以在一个步骤中完成:

data = list(in_file)
但实际上,您首先需要这个列表的唯一原因是,您可以稍后循环使用它,创建输出文件,对吗?那么,为什么不先暂停,然后循环文件中的行呢

无论您如何生成输出内容,都要先这样做。然后用文件名列表在文件上循环并写入内容。像这样:

paths = [os.path.join(dir, '{}.xls'.format(name)) for name in data]
out_files = [open(path, 'w') for path in paths]
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\u throughput
的函数,该函数在本地导入一些模块,然后什么都不做)。

那么您的问题是什么?另外,请减少代码示例的损坏。为什么要将每个名称分配给全局名称?然而,不管你怎么做,你仍然有我的列表,所以为什么不在我的列表中使用名称:
?我做了一点修改。我为一个示例显示的代码有我的区域,我在其中创建了名称列表,它在文件中按行提取数据。我猜这个名为“数据”的列表将相当于“我的列表”,如果我这样做了,我很抱歉用词会很奇怪。有时我很难解释我想做什么。你是在Excel工作簿中放了什么东西,还是只是空文件?