简体   繁体   English

如何从文件中打印出符合特定条件但有许多列要检查的行?

[英]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,则打印它。

  • no substring issue: checks for exact field 没有子字符串问题:检查确切的字段
  • checks for field at the "win" column only, not the other ones. 仅检查“获胜”列中的字段,而不检查其他字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您如何阅读文本文件并只打印列出的某些行? - How do you read a text file and only print certain lines listed together? 如何使用 for 循环在新行上打印出 Numpy 数组中的元素? - How do you print out elements from a Numpy array on new lines using a for loop? 你如何检查文件是否被修改,在一定的时间间隔内? - How do you check if files have been modified, in a certain intervall? 如何替换文本文件中的某些行? - How do you replace certain lines in a text file? Python:如何根据列名称中的子字符串匹配从数据集中过滤出列 - Python : How do you filter out columns from a dataset based on substring match in Column names 如何在Python中打印出文件中的某些行 - How to print out certain lines in a file in Python 如何在特定条件下打印数字? - How do you print numbers with certain conditions? 如何从wx.TextCtrl打印出一个值? - How do you print out a value from wx.TextCtrl? 您如何检查字符串是否包含列表中的内容并打印出来? - How do you check if a string has something from the list and print it? 如何打印 if else 语句中某个参数被满足的次数? - How do you print how many times a certain parameter was met in an if else statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM