简体   繁体   English

打印行搜索的输出

[英]Printing the output of a Line search

I'm new to programming pretty much in general and I am having difficulty trying to get this command to print it's output to the .txt document. 一般而言,我是编程新手,尝试获取此命令以将其输出打印到.txt文档时遇到困难。 My goal in the end is to be able to change the term "Sequence" out for a variable where I can integrate it into a custom easygui for multiple inputs and returns, but that's a story for later down the road. 最后,我的目标是能够将变量“序列”更改为一个变量,在该变量中我可以将其集成到自定义的easygui中以进行多个输入和返回,但这是以后的故事。 For the sake of testing and completion of the current project I will be just manually altering the term. 为了测试和完成当前项目,我将手动更改术语。

I've been successful in being able to get another program to send it's output to a .txt but this one is being difficult. 我已经成功地获得了另一个程序,可以将其输出发送到.txt,但这很难。 I don't know if I have been over looking something simple, but I have been grounded for more time than I would like to have been on this. 我不知道我是否一直在寻找简单的东西,但是我在地面上的时间比我想要的更多。

When the it searches for the lines it prints the fields in the file I want, however when it goes to write it finds the last line of the file and then puts that in the .txt as the output. 当它搜索行时,它将在我想要的文件中打印字段,但是当它写时,它会找到文件的最后一行,然后将其放在.txt中作为输出。 I know the issue but I haven't been able to wrap my head around how to fix it, mainly due to my lack of knowledge of the language I think. 我知道这个问题,但是我仍然无法解决该问题,这主要是由于我对我认为的语言缺乏了解。

I am using Sublime Text 2 on Windows 我在Windows上使用Sublime Text 2

def main():
    import os

    filelist = list()

    filed = open('out.txt', 'w')
    searchfile = open("asdf.csv")

    for lines in searchfile:
        if "Sequence" in lines:
         print lines

    filelist.append(lines)

    TheString = " ".join(filelist)

    searchfile.close()

    filed.write(TheString)

    filed.close()

main()

It sounds like you want to the lines you are printing out collected in the variable "filelist", which will then be printed to the file at the .write() call. 听起来好像您希望将要打印的行收集在变量“文件列表”中,然后将在.write()调用中将其打印到文件中。 Only a difference of indentation (which is significant in Python) prevents this from happening: 只有缩进的不同(在Python中很重要)可以防止这种情况的发生:

def main():
    import os

    filelist = list()

    filed = open('out.txt', 'w')
    searchfile = open("asdf.csv")

    for lines in searchfile:
        if "Sequence" in lines:
           print lines
           filelist.append(lines)

    TheString = " ".join(filelist)

    searchfile.close()

    filed.write(TheString)

    filed.close()

main()

Having

           filelist.append(lines)

at the same level of indentation as 缩进程度与

           print lines

tells Python that they are in the same block, and that the second statement also belongs to the "then" clause of the if statement. 告诉Python它们在同一块中,第二条语句也属于if语句的“ then”子句。

Your problem is that you are not appending inside the loop, as a consequence you are only appending the last line, do like this: 您的问题是您没有在循环内追加,结果是您仅追加了最后一行,请执行以下操作:

for lines in searchfile:
    if "Sequence" in lines:
       print lines
       filelist.append(lines)

BONUS : This is the " pythonic " way to do what you want: 奖励 :这是您执行所需操作的“ pythonic ”方法:

def main():
    with open('asdf.csv', 'r') as src, open('out.txt', 'w') as dest:
        dest.writelines(line for line in src if 'sequence' in line)
def main():

    seq = "Sequence"

    record = file("out.txt", "w")
    search = file("in.csv", "r")

    output = list()

    for line in search:
        if seq in line: output.append(line)

    search.close()

    record.write(" ".join(output))
    record.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM