每次对for循环中的项目执行功能时,如何创建不同的文件?
How to create different files for each time a function is performed on an item in a for loop?
因此,这里的一位社区成员帮助我 fine-tune 创建了一个函数,该函数从文本文件中获取字典值,然后查看该值以查找文件列表。这是我将找到的所有值附加到相同 sheet 的代码。
import csv
import glob
import ast
from os.path import isfile
from lxml import etree
def look_for_speaker_in_files(speakerAttrib):
speakerDict = ast.literal_eval(speakerAttrib)
l_file_exists = False
if isfile("allspeakers.csv"):
l_file_exists = True
c = csv.writer(open("allspeakers.csv","a"))
if not l_file_exists:
c.writerow(["Name", "Filename", "Text"])
lparser = etree.XMLParser(recover=True)
for cr_file in glob.iglob('parsed/*.xml'):
try:
tree = etree.parse(cr_file,parser=lparser)
for node in tree.iter('speaking'):
if node.keys() == speakerDict.keys():
c.writerow([node.attrib, cr_file, node.text])
else:
continue
except:
print "bad string " + cr_file
raise
def main():
with open("speaking-basic.txt","r") as speaker_list:
for x in speaker_list:
print x
look_for_speaker_in_files(x)
if __name__ == "__main__":
main()
现在我想为文本文件中的每个项目创建一个不同的文件,并且只将文件中节点的匹配项存放到文本文件中的那个项目。
我尝试了下面的代码,我在其中更改了读取文件的函数,以尝试让它同时采用文件名参数和要搜索的文本文件中的项目参数。然后,我更改了从列表中获取项目的代码,以从列表中读取项目,并让它创建一个文件名,该文件名是文件中字典项目的值。
import csv
import glob
import ast
from os.path import isfile
from lxml import etree
def look_for_speaker_in_files(speakerAttrib,file_name):
speakerDict = ast.literal_eval(speakerAttrib)
l_file_exists = False
if isfile( file_name + ".csv"):
l_file_exists = True
c = csv.writer(open( file_name + ".csv","a"))
print c
if not l_file_exists:
c.writerow(["Name", "Filename", "Text"])
lparser = etree.XMLParser(recover=True)
for cr_file in glob.iglob('parsed/*.xml'):
try:
tree = etree.parse(cr_file,parser=lparser)
for node in tree.iter('speaking'):
if node.keys() == speakerDict.keys():
c.writerow([node.attrib, cr_file, node.text])
else:
continue
except:
print "bad string " + cr_file
raise
def main():
with open("speaking.txt","r") as speaker_list:
for x in speaker_list:
print x
dictx = ast.literal_eval(x)
valuex = str(dictx.values()).format()
print valuex
look_for_speaker_in_files(x,valuex)
if __name__ == "__main__":
main()
我还尝试将文件查找功能保持为同一个参数,但添加了一个从该参数得出文件名的变量,但这也没有用。
import csv
import glob
import ast
from os.path import isfile
from lxml import etree
def look_for_speaker_in_files(speakerAttrib):
speakerDict = ast.literal_eval(speakerAttrib)
file_name = str(speakerDict.values()).format()
l_file_exists = False
if isfile(file_name + ".csv"):
l_file_exists = True
c = csv.writer(open(file_name + ".csv","a"))
if not l_file_exists:
c.writerow(["Name", "Filename", "Text"])
lparser = etree.XMLParser(recover=True)
for cr_file in glob.iglob('parsed/*.xml'):
try:
tree = etree.parse(cr_file,parser=lparser)
for node in tree.iter('speaking'):
if node.keys() == speakerDict.keys():
c.writerow([node.attrib, cr_file, node.text])
else:
continue
except:
print "bad string " + cr_file
raise
def main():
with open("speaking-basic.txt","r") as speaker_list:
for x in speaker_list:
print x
look_for_speaker_in_files(x)
if __name__ == "__main__":
main()
虽然这些代码创建了文件,但经过我的所有努力,我似乎只能让它在文件中只放 headers 列,或者将所有扬声器从xml 每个文件中的文件,而不仅仅是我放入函数中的文件。此外,如果文件名周围没有 [''],我无法让它创建文件名。
我有函数正在搜索的 xml 文件和文本文件,其中包含我在 google link 中查找的值:
https://drive.google.com/open?id=0B7lGA34vOZItREhRbmF6Z3YtTnM
如有任何帮助,我们将不胜感激,谢谢!
我不确定我是否正确理解了您的要求。现在这段代码所做的是,根据 speaking-basic.txt 中给出的属性,在解析的 xml 中找到属性。如果属性名称及其值在 speaking-basic.txt 和 xml 中都匹配,则将其输出到具有 Speaker.
名称的文件中
检查这是否是您要查找的内容,也许我没有理解正确。如果是这样,请通过屏幕截图或示例清楚地了解预期输出。
import csv
import glob
import ast
from os.path import isfile
from lxml import etree
def look_for_speaker_in_files(speakerAttrib):
speakerDict = ast.literal_eval(speakerAttrib)
file_name = str(speakerDict.values()[0]).format()
l_file_exists = False
if isfile(file_name + ".csv"):
l_file_exists = True
c = csv.writer(open(file_name + ".csv","a"))
if not l_file_exists:
c.writerow(["Name", "Filename", "Text"])
lparser = etree.XMLParser(recover=True)
for cr_file in glob.iglob('parsed/*.xml'):
try:
tree = etree.parse(cr_file,parser=lparser)
for node in tree.iter('speaking'):
if node.keys() == speakerDict.keys():
if node.values() == speakerDict.values():
c.writerow([node.attrib, cr_file, node.text])
else:
continue
else:
continue
except:
print "bad string " + cr_file
raise
def main():
with open("speaking-basic.txt","r") as speaker_list:
for x in speaker_list:
print x
look_for_speaker_in_files(x)
if __name__ == "__main__":
main()
因此,这里的一位社区成员帮助我 fine-tune 创建了一个函数,该函数从文本文件中获取字典值,然后查看该值以查找文件列表。这是我将找到的所有值附加到相同 sheet 的代码。
import csv
import glob
import ast
from os.path import isfile
from lxml import etree
def look_for_speaker_in_files(speakerAttrib):
speakerDict = ast.literal_eval(speakerAttrib)
l_file_exists = False
if isfile("allspeakers.csv"):
l_file_exists = True
c = csv.writer(open("allspeakers.csv","a"))
if not l_file_exists:
c.writerow(["Name", "Filename", "Text"])
lparser = etree.XMLParser(recover=True)
for cr_file in glob.iglob('parsed/*.xml'):
try:
tree = etree.parse(cr_file,parser=lparser)
for node in tree.iter('speaking'):
if node.keys() == speakerDict.keys():
c.writerow([node.attrib, cr_file, node.text])
else:
continue
except:
print "bad string " + cr_file
raise
def main():
with open("speaking-basic.txt","r") as speaker_list:
for x in speaker_list:
print x
look_for_speaker_in_files(x)
if __name__ == "__main__":
main()
现在我想为文本文件中的每个项目创建一个不同的文件,并且只将文件中节点的匹配项存放到文本文件中的那个项目。
我尝试了下面的代码,我在其中更改了读取文件的函数,以尝试让它同时采用文件名参数和要搜索的文本文件中的项目参数。然后,我更改了从列表中获取项目的代码,以从列表中读取项目,并让它创建一个文件名,该文件名是文件中字典项目的值。
import csv
import glob
import ast
from os.path import isfile
from lxml import etree
def look_for_speaker_in_files(speakerAttrib,file_name):
speakerDict = ast.literal_eval(speakerAttrib)
l_file_exists = False
if isfile( file_name + ".csv"):
l_file_exists = True
c = csv.writer(open( file_name + ".csv","a"))
print c
if not l_file_exists:
c.writerow(["Name", "Filename", "Text"])
lparser = etree.XMLParser(recover=True)
for cr_file in glob.iglob('parsed/*.xml'):
try:
tree = etree.parse(cr_file,parser=lparser)
for node in tree.iter('speaking'):
if node.keys() == speakerDict.keys():
c.writerow([node.attrib, cr_file, node.text])
else:
continue
except:
print "bad string " + cr_file
raise
def main():
with open("speaking.txt","r") as speaker_list:
for x in speaker_list:
print x
dictx = ast.literal_eval(x)
valuex = str(dictx.values()).format()
print valuex
look_for_speaker_in_files(x,valuex)
if __name__ == "__main__":
main()
我还尝试将文件查找功能保持为同一个参数,但添加了一个从该参数得出文件名的变量,但这也没有用。
import csv
import glob
import ast
from os.path import isfile
from lxml import etree
def look_for_speaker_in_files(speakerAttrib):
speakerDict = ast.literal_eval(speakerAttrib)
file_name = str(speakerDict.values()).format()
l_file_exists = False
if isfile(file_name + ".csv"):
l_file_exists = True
c = csv.writer(open(file_name + ".csv","a"))
if not l_file_exists:
c.writerow(["Name", "Filename", "Text"])
lparser = etree.XMLParser(recover=True)
for cr_file in glob.iglob('parsed/*.xml'):
try:
tree = etree.parse(cr_file,parser=lparser)
for node in tree.iter('speaking'):
if node.keys() == speakerDict.keys():
c.writerow([node.attrib, cr_file, node.text])
else:
continue
except:
print "bad string " + cr_file
raise
def main():
with open("speaking-basic.txt","r") as speaker_list:
for x in speaker_list:
print x
look_for_speaker_in_files(x)
if __name__ == "__main__":
main()
虽然这些代码创建了文件,但经过我的所有努力,我似乎只能让它在文件中只放 headers 列,或者将所有扬声器从xml 每个文件中的文件,而不仅仅是我放入函数中的文件。此外,如果文件名周围没有 [''],我无法让它创建文件名。
我有函数正在搜索的 xml 文件和文本文件,其中包含我在 google link 中查找的值: https://drive.google.com/open?id=0B7lGA34vOZItREhRbmF6Z3YtTnM
如有任何帮助,我们将不胜感激,谢谢!
我不确定我是否正确理解了您的要求。现在这段代码所做的是,根据 speaking-basic.txt 中给出的属性,在解析的 xml 中找到属性。如果属性名称及其值在 speaking-basic.txt 和 xml 中都匹配,则将其输出到具有 Speaker.
名称的文件中检查这是否是您要查找的内容,也许我没有理解正确。如果是这样,请通过屏幕截图或示例清楚地了解预期输出。
import csv
import glob
import ast
from os.path import isfile
from lxml import etree
def look_for_speaker_in_files(speakerAttrib):
speakerDict = ast.literal_eval(speakerAttrib)
file_name = str(speakerDict.values()[0]).format()
l_file_exists = False
if isfile(file_name + ".csv"):
l_file_exists = True
c = csv.writer(open(file_name + ".csv","a"))
if not l_file_exists:
c.writerow(["Name", "Filename", "Text"])
lparser = etree.XMLParser(recover=True)
for cr_file in glob.iglob('parsed/*.xml'):
try:
tree = etree.parse(cr_file,parser=lparser)
for node in tree.iter('speaking'):
if node.keys() == speakerDict.keys():
if node.values() == speakerDict.values():
c.writerow([node.attrib, cr_file, node.text])
else:
continue
else:
continue
except:
print "bad string " + cr_file
raise
def main():
with open("speaking-basic.txt","r") as speaker_list:
for x in speaker_list:
print x
look_for_speaker_in_files(x)
if __name__ == "__main__":
main()