简体   繁体   English

如何在Python 3.2中的txt文件中记录“ Y”的出现

[英]How do i record the occurances of 'Y' in a txt file in Python 3.2

I'm trying to record how many times the letter Y appears in the 6th column of a .txt file and assign that to a variable. 我试图记录字母Y在.txt文件的第六列中出现多少次,并将其分配给变量。 How would I go about doing that? 我将如何去做? Thx in advance! 提前谢谢!

.txt file: .txt文件:

119,29/12/15,18:00,Ripley,Magnitude,Y,Magnitude               
120,30/12/15,18:00,Jenkins,Kes,Y,Kes                    
121,31/12/15,18:00,Big Boss,Ingsoc,Y,Ingsoc                
122,01/01/16,18:00,Einstein100,RNGesus,,             
123,02/01/16,18:00,Macho Man,Napoleon Wilson,,               
124,03/01/16,18:00,Dennis,Billy Casper,, 

I've set the whole file to a variable and have created variables for each column in the file. 我将整个文件设置为变量,并为文件中的每一列创建了变量。

    NextRecord = data[x]
    Number = NextRecord[0]
    Date = NextRecord[1]
    Time = NextRecord[2]
    P1Nickname = NextRecord[3]
    P2Nickname = NextRecord[4]
    Status = NextRecord[5]

Use csv and sum : 使用csvsum

import csv

with open(filename) as records:
    reader = csv.reader(records)
    print(sum(row[5] == 'Y' for row in reader))

Your data is comma separated, and Python has the csv module for working with delimited values (could be semicolon separated, etc, as well). 您的数据以逗号分隔,Python具有用于分隔值的csv模块(也可以以分号分隔,等等)。

The reader is iterable and yields a row at a time. 阅读器是可迭代的,并且每次产生一行。

I've used the sum function which takes an iterable but I've used a generator expression to yield True or False for each row in the file. 我使用了sum函数,该函数具有可迭代性,但是我使用了生成器表达式为文件中的每一row生成TrueFalse

The expression row[5] == 'Y' evaluates either True or False . 表达式row[5] == 'Y'计算TrueFalse When you give a boolean value to sum it converts to an integer and True becomes 1 , False becomes 0 . 当您给布尔值sum它将转换为整数,并且True变为1False变为0 We could have written it as: 我们可以这样写:

sum(1 for row in reader
    if row[5] == 'Y')

This however is slightly different. 但是,这略有不同。 It filters out only the rows which have Y in the 6th column and yields the value 1 for each of those rows. 它仅过滤出第6列中具有Y的行,并为每行得出值1 It's subtly different. 完全不同。

You should try using the csv module: 您应该尝试使用csv模块:

import csv

counter = 0
with open("data.csv","r") as fi:
    inCsv = csv.reader(fi,delimiter=',')
    for row in inCsv:
        if (row[5].strip() == 'Y'):
            counter += 1
print ("Y count: " + str(counter))

THIS should work as expected: 这应该按预期工作:

noOfY=0
for line in open("thatTextFile.txt"): 
    try: 
        if 'Y' in line.split(',')[5]: noOfY+=1
    except:
        pass
print(noOfY)

This gives for the text provided above: 这给出了上面提供的文本:

3

and works even in case of malformed lines in the input text file. 并且即使输入文本文件中的行格式错误也可以使用。

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

相关问题 如何将一个csv(txt)文件中的记录与另一个csv(txt)文件中的记录进行比较,并根据它们的比较计算数字? - How do I compare a record in a csv (txt) file to a record in another csv (txt) file and calculate a number based on their comparison? Python:如何创建文件.txt并在其中记录信息 - Python: how to create a file .txt and record information in it Python。 我如何从txt文件中拆分()? - Python. how do i do split() from txt file? 如何在 python 中将 las 文件转换为 txt(或 csv)文件? - How do i convert a las file into a txt (or csv) file in python? 如何在 .txt 文件中指定删除短语,其中一个短语包含“x”,另一个“x”和“y”(其中 x 被删除,但 x 和 y 不) - How do I specify deleting phrases in a .txt file with one phrase containing 'x' the other 'x' and 'y' (in which x to be deleted but x and y not) 如何在for循环(Python)中写入txt文件? - How do I write to txt file in for loop (Python)? Python:如何使用open()函数清除.txt文件? - Python: How do I clear a .txt file with the open() function? 如何在我的 Python 代码中添加和打印 txt 文件? - How do i add and print a txt file in my Python code? 如何从 python 中的 a.txt 文件加载矩阵? - How do I load a matrix from a .txt file in python? 如何在Python中打开.txt文件并创建XML标签? - How do I open a .txt file and create XML tags in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM