LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   extracting particular lines from a text file (https://www.linuxquestions.org/questions/programming-9/extracting-particular-lines-from-a-text-file-679471/)

abolishtheun 10-28-2008 12:06 PM

Quote:

Originally Posted by ghostdog74 (Post 3324156)
you judge for yourself.

Yet yours is less powerful than the example I gave, which can take in input from either STDIN or arbitrarily many files given at the command line.

Sergei Steshenko 10-28-2008 12:07 PM

Quote:

Originally Posted by ghostdog74 (Post 3324156)
Code:

linenumbers = open("file_num").readlines()
linenumbers = [i.strip() for i in linenumbers]
for line in open("file_data"):
    num,content = line.split(":")
    if num in linenumbers:
        print line.strip()

you judge for yourself.


that's quite true (although i don't quite like the way you put your braces). however, with the help of language syntax and features, it CAN be even clearer.

Again, it's people - "i" is unreadable in "[i.strip() for i in linenumbers]" - if I understand Python correctly, it should be

linenumbers = [line.strip() for line in linenumbers]
.

Regarding my braces - if you just ignore them, you'll get the same indentation as Python forces you to use.

ghostdog74 10-28-2008 12:27 PM

Quote:

Originally Posted by abolishtheun (Post 3324166)
Yet yours is less powerful than the example I gave, which can take in input from either STDIN or arbitrarily many files given at the command line.

do you need me to make it more "powerful"?

ghostdog74 10-28-2008 12:31 PM

Quote:

Originally Posted by Sergei Steshenko (Post 3324168)
Regarding my braces - if you just ignore them, you'll get the same indentation as Python forces you to use.

try ignoring them for big projects with few thousand LOC. I am also not about to get deeper into this, so you all can carry on, i'm out.


All times are GMT -5. The time now is 03:58 PM.