2013-02-12 294 views
1

因此,我正在导入名称列表 例如创建for循环命名输出文件Python

文本文件将包括:

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() 
+2

那么你的问题是什么?另外,请尽量减少代码示例。 – millimoose 2013-02-12 19:23:27

+0

为什么你将每个名称分配给全局?然而,无论如何,为什么你这样做,你仍然拥有'my_list',那么为什么不直接使用'作为my_list中的名字:'? – abarnert 2013-02-12 19:24:18

+0

我改变了一点点。我为示例显示的代码有我的区域,我在其中创建名称列表,并在文件中逐行提取数据。我想这个名为“数据”的列表将等于“my_list” – 2013-02-12 19:27:39

回答

0

我还是不知道你想做什么(并以“不知道”,我的意思是“彻底难倒了”),但我想我可以解释一些你做错了什么,怎么做是正确的:

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文件一样轻松地处理它们。或者,您可以使用win32compywinauto来控制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该功能导入本地一些模块,然后什么都不做)。

0

看看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)) 
0

试试这个:

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