Python 如何计算字符串在文件中的出现次数并将其附加到另一个文件中
我需要计算.txt文件中出现“Product ID”的次数,并让它打印该文件中的数字。我对python还不熟悉,我正试图了解这一点。我让它在代码中单独工作,但它在运行程序后将数字打印到命令行(因此打印)。我尝试使用print(count)>>“hardDriveSummary.txt文件”和print>>count“hardDriveSummary.txt文件”,但无法使其工作Python 如何计算字符串在文件中的出现次数并将其附加到另一个文件中,python,file,count,Python,File,Count,我需要计算.txt文件中出现“Product ID”的次数,并让它打印该文件中的数字。我对python还不熟悉,我正试图了解这一点。我让它在代码中单独工作,但它在运行程序后将数字打印到命令行(因此打印)。我尝试使用print(count)>>“hardDriveSummary.txt文件”和print>>count“hardDriveSummary.txt文件”,但无法使其工作 # Read .xml file and putlines row_name and Product ID into
# Read .xml file and putlines row_name and Product ID into new .txt file
search = 'row_name', 'Product ID'
#source file
with open('20190211-131516_chris_Hard_Drive_Order.xml') as f1:
#output file
with open('hardDriveSummary.txt', 'wt') as f2:
lines = f1.readlines()
for i, line in enumerate(lines):
if line.startswith(search):
f2.write("\n" + line)
#count how many occurances of 'Product ID' in .txt file
def main():
file = open('hardDriveSummary.txt', 'r').read()
team = "Product ID"
count = file.count(team)
print(count)
main()
hardDriveSummary.txt的示例:
Name Country 1
Product ID : 600GB
Name Country 2
Product ID : 600GB
Name Country 1
Product ID : 450GB
.xml文件的内容:
************* Server Summary *************
Server serv01
label R720
asset_no CNT3NW1
Name Country 1
name.1 City1
Unnamed: 6 NaN
************* Drive Summary **************
ID : 0:1:0
State : Failed
Product ID : 600GB
Serial No. : 6SL5KF5G
************* Server Summary *************
Server serv02
label R720
asset_no BZYGT03
Name Country 2
name.1 City2
Unnamed: 6 NaN
************* Drive Summary **************
ID : 0:1:0
State : Failed
Product ID : 600GB
Serial No. : 6SL5K75G
************* Server Summary *************
Server serv03
label R720
asset_no 5GT4N51
Name Country 1
name.1 City1
Unnamed: 6 NaN
************* Drive Summary **************
ID : 0:1:0
State : Failed
Product ID : 450GB
Serial No. : 6S55K5MG
如果您只是想将计数器值标记到文件的末尾,那么以下代码应该可以工作:
import os
def main():
with open('hardDriveSummary.txt', 'ab+') as f:
term = "Product ID"
count = f.read().count(term)
f.seek(os.SEEK_END) # Because we've already read the entire file. Go to the end before writing otherwise we get an IOError
f.write('\n'+str(count))
如果您只是想将计数器值标记到文件的末尾,那么以下代码应该可以工作:
import os
def main():
with open('hardDriveSummary.txt', 'ab+') as f:
term = "Product ID"
count = f.read().count(term)
f.seek(os.SEEK_END) # Because we've already read the entire file. Go to the end before writing otherwise we get an IOError
f.write('\n'+str(count))
由于
Product ID
是两个不同的单词,因此将整个文本拆分为两个词组,以下代码将给出预期结果:
from collections import Counter
f = open(r"sample.py", "r")
words = f.read().split()
bigrams = zip(words, words[1:])
counts = Counter(bigrams)
data = {' '.join(k): v for k, v in dict(counts).items()}
if 'Product ID' in data:
print('Count of "Product ID": ', data['Product ID'])
由于
Product ID
是两个不同的单词,因此将整个文本拆分为两个词组,以下代码将给出预期结果:
from collections import Counter
f = open(r"sample.py", "r")
words = f.read().split()
bigrams = zip(words, words[1:])
counts = Counter(bigrams)
data = {' '.join(k): v for k, v in dict(counts).items()}
if 'Product ID' in data:
print('Count of "Product ID": ', data['Product ID'])
是“Product ID”两个不同的单词shi@Jeril,它是在进入xml文件之前最初从数据库中提取的两个不同的单词。请检查我的解决方案是“Product ID”两个不同的单词shi@Jeril,它是数据库中两个不同的单词,在进入xml文件之前,它最初是从数据库中提取的。请检查我的解决方案谢谢@culzie。我将您的部分添加到代码中,但它没有显示在.txt文件中。我已经在我的帖子中添加了一个文件的示例,以防它有所不同。再次感谢!你提供的文本文件对我来说很好。你能用xml片段更新这个问题吗,这样我就可以运行整个脚本了?还有,您使用的是什么版本的python?嗨@culzie,我已经添加了xml文件的内容。我也不得不擦洗它。好的,我现在已经运行了整个过程,它仍在将计数写入文件。需要注意的是,您的.xml文件不是xml格式。您应该将其重命名为.txt文件以避免混淆。我这边的输出和示例数据:`Product ID:600GB Product ID:600GB Product ID:450GB 3`Hi@CULZIE。我在末尾尝试了main(),没有(像你的例子一样),没有main(),我什么也得不到,但是有了main(),我得到了“0”:(谢谢@culzie。我在代码中添加了您的部分,但它没有显示在.txt文件中。我在帖子中添加了一个文件示例,以防它产生影响。再次感谢!您提供的文本文件对我来说效果很好。您能用xml片段更新问题,以便我可以运行整个脚本吗?还有,您使用的python版本是什么u using?Hi@culzie,我已经添加了xml文件的内容。我也必须清理它。好的,我现在已经运行了整个过程,它仍在将计数写入文件。需要注意的是,您的.xml文件不是xml格式。您应该将其重命名为.txt文件以避免混淆。在我这边,使用示例数据输出:`Product ID:`:600GB产品ID:600GB产品ID:450GB 3`Hi@cullzie。我尝试过在末尾使用main()而不使用main()(如您的示例),没有main()我什么也得不到,但使用main()我得到“0”:(