[英]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]
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
生成True
或False
。
The expression row[5] == 'Y'
evaluates either True
or False
. 表达式row[5] == 'Y'
计算True
或False
。 When you give a boolean value to sum
it converts to an integer and True
becomes 1
, False
becomes 0
. 当您给布尔值sum
它将转换为整数,并且True
变为1
, False
变为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.