簡體   English   中英

如何查找字符串在CSV文件中出現多少次

[英]How to find how many times a string appears in CSV file

我需要知道一個字符串在CSV文件中出現了多少次。 我的代碼沒有給我正確的號碼。

import csv
import re
def main():

    print('Here is the File Information.')
    print('_' * 29)
    infile = open('employee_payroll.csv', 'r').read()



    count = 0
    string = 'Board of Regents'
    for string in infile:
        count+= 1

這樣的事情應該有助於解決您遇到的問題。 您可能會發現需要對數據進行試驗。 數據是否有\\n字符? 是逗號,制表符分隔嗎? 無論下面的代碼如何幫助您。

count = 0
with open(file) as  f:
    for line in f:
        for word in line.split('with_my_delimiter'):
            if word == 'my_word':
                count = count + 1

有一個.count()函數

csv = """
Foo, 20, Berlin
Bar, 23, Paris
Max, 44, New York
Foo, 74, Sydney
"""

print csv.count('Foo')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM