I have a file I'm downloading via FTP. It's a very large file, so I want to only get the first say, 20 lines to work with right now. I want to write those 20 lines to a new file on my local machine. In the process, I want to match on a value within the line.
The file is pipe-delimited and the the beginning of each line looks like this:
9999-12-31|XX|...
I want to only write to the output file when the value of that second field is XX, otherwise, ignore it.
Here are the basics of my code:
def writeline(line):
file.write(line + "\n")
file = open(localDir + fileName, "w+")
ftp.retrlines("RETR '" + remotePath + "'", writeline)
All of this code works fine to download the file if I want to output the entire file. I tried to put a while loop into my writeline
function, but it would just write each line the number of times I specified in my loop, which makes sense in hindsight. It seems like the while loop needs to be somehow in the retrlines
function.
I'm pretty new to Python, so I appreciate any help you can provide and for your patience with my noob question.
Update Ok, it looks like to match on the substring, I can do:
line[11:13]
but that still leaves me with the problem of trying to get only the first x lines to work with.
Try to open your file in an other way, something like: