[英]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. 基本上,这是一个名为firesideResults
的文件,我在代码中打开该文件,我必须对其进行检查。 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. 如果win列包含0,则不会将其打印出来,因此,如果它包含非零的数字,则会在屏幕上显示它。 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. 但是不幸的是,其他代码列之一包含一个0,所以我不能这样做。 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
! 由于您使用逗号分隔字段,因此最好的方法是使用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. 如果每行的第三列不为0,则打印它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.