简体   繁体   中英

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. 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. 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. 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. 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

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. Only a difference of indentation (which is significant in Python) prevents this from happening:

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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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