简体   繁体   中英

How do you print out lines from a file that match a certain condition, but you have many columns to check?

name played wins loses
Leeroy,19,7,12
Jenkins,19,8,11
Tyler,19,0,19
Napoleon Wilson,19,7,12
Big Boss,19,7,12
Game Dude,19,5,14
Macho Man,19,3,16
Space Pirate,19,6,13
Billy Casper,19,7,12
Otacon,19,7,12
Big Brother,19,7,12
Ingsoc,19,5,14
Ripley,19,5,14
M'lady,19,4,15
Einstein100,19,8,11
Dennis,19,5,14
Esports,19,8,11
RNGesus,19,7,12
Kes,19,9,10
Magnitude,19,6,13

Basically, this is a file called firesideResults , which i open in my code and i have to check through it. If the win column contains a 0 i do not print it out, so if it contains a number other than zero i display it on the screen. However, i have multiple lists of numbers to deal with and i can't find how to only deal with one column of numbers.

my code was going to be

if option == ("C") or option == ("c"):
    answer = False
    file_3 = open("firesideResults.txt")
    for column in file_3:
        if ("0" not in column):
            print(column)

But unfortunately, one of the other columns of code contain a 0 so i cannot do that. Thank you for your help and if possible please list any questions that i could check for help as i have been searching for so long.

Since you have comma-separated fields, the best way would be to use csv !

import csv

with open("firesideResults.txt") as file_3:
    cr = csv.reader(file_3)
    for row in cr:
        if row[2]!="0":
            print(row)

if third column of each row is not 0, then print it.

  • no substring issue: checks for exact field
  • checks for field at the "win" column only, not the other ones.

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