[英]Python: collect data from multiple lines at a time and print the results in order
I'm using cucumber on my project and to keep track of all our steps I've written a small script in Python that seeks out the feature files in a certain directory and get me the steps based on the gherkin keywords. 我在项目上使用了黄瓜,并跟踪了所有步骤,我用Python编写了一个小脚本,该脚本在某个目录中查找功能文件,并根据gherkin关键字获取步骤。 Below or above these steps we're trying to put a description of what each step does. 在这些步骤的上方或下方,我们试图描述每个步骤的作用。 When I collect all these steps I want to be able to also print the instructions for the respective steps. 当我收集所有这些步骤时,我希望能够同时打印各个步骤的说明。
import os
searchPath = "somePath"
dirs = os.listdir(searchPath)
givenLine = []
whenLine = []
thenLine = []
for root, dirs, files in os.walk(searchPath):
newFile = open("filePath", "w")
for file in files:
if file.endswith(".feature"):
with open(os.path.join(root, file)) as featureFile:
linesOfFiles = featureFile.readlines()
for lineOfFile in linesOfFiles:
if "Given" in lineOfFile and "#" not in lineOfFile:
givenLine.append(lineOfFile.strip())
continue
if "instructions" in lineOfFile:
givenLine.append(lineOfFile.strip())
continue
if "more instructions" in lineOfFile:
givenLine.append(lineOfFile.strip())
if "When" in lineOfFile and "#" not in lineOfFile:
givenLine.append(lineOfFile.strip())
(same code as above)
if "Then" in lineOfFile and "#" not in lineOfFile:
thenLine.append(lineOfFile.strip())
(same code as above)
for linesToWrite in givenLine, whenLine, thenLine:
newFile.writelines("\nStep: " .join(list(sorted(set(linesToWrite )))))
Running the code above gives me the results but the list is not preserved. 运行上面的代码可以得到结果,但列表未保留。
I do not fully understand your question so I can probably not provide the right answer. 我无法完全理解您的问题,因此我可能无法提供正确的答案。 But can you not just: 但是,您不仅可以:
searchPath = "somePath"
dirs = os.listdir(searchPath)
myLines= []
for root, dirs, files in os.walk(searchPath):
newFile = open("filePath", "a")
for file in files:
if file.endswith(".feature"):
with open(os.path.join(root, file)) as featureFile:
linesOfFiles = featureFile.readlines()
for lineOfFile in linesOfFiles:
lineDict = {}
if "Given" in lineOfFile and "#" not in lineOfFile:
lineDict["givenLine"] = lineOfFile.strip())
continue
if "instructions" in lineOfFile:
lineDict["instructionsLine"] = lineOfFile.strip()
continue
if "more instructions" in lineOfFile:
lineDict["moreInstructions"] = lineOfFile.strip())
if "When" in lineOfFile and "#" not in lineOfFile:
lineDict["when"] = lineOfFile.strip())
(same code as above)
if "Then" in lineOfFile and "#" not in lineOfFile:
lineDict["then"] = lineOfFile.strip()
(same code as above)
myLines.append(lineDict)
for linesToWrite in myLines:
if linesToWrite["givenLine"]:
newFile.write(linesToWrite["givenLine"])
if linesToWrite["instructionsLine"]:
newFile.write(linesToWrite["instructionsLine"])
#etc....
You also dont have to this: 您也不必这样做:
linesOfFiles = featureFile.readlines()
This is enough: 这就够了:
for lineOfFile in featureFile:
Also to make your live easy, you can open two files with "with open": 为了使您的生活更加轻松,您可以使用“ with open”打开两个文件:
with open('inputfile', 'r') as inputFile, open('outputfile', 'a') as output:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.