Counting the occurence of a specific string in a csv file

Finlandia_C

Hi I have a file in which I need to count how many times the word 'Methadone' and 'Co-Codamol' appear. It seems to print '0' when in fact in this particular file the total times methadone and co-codamol occur is 9 times. Here is what the file looks like:

http://i.stack.imgur.com/hUdJt.png

and here is my code:

import csv
import collections
number = collections.Counter()
with open('C:\Users\Desktop\practice jan.csv') as input_file:
    for row in csv.reader(input_file, delimiter=','):
        number[row[1]] += 1
print 'Number of prescriptions is %s' % number['Methadone' + 'Co-Codamol']

>> Number of opioid prescriptions is 0
MarcelSimon

Read the file and count the number of occurences of your string:

with open('Path/to/file', 'r') as content_file:
    content = content_file.read()
    print(content.count("Methadone"))

As long as your file is not too large, this will do the work.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Counting occurence within a string Python

From Dev

PHP: Counting Extensions Occurence on a String

From Dev

Counting occurence of a word in a text file using R

From Dev

counting occurence of a word in a text file using python

From Dev

Counting occurrences of a string in a column of a csv file

From Dev

Targeting a specific occurence in a string and replacing it

From Dev

Counting occurence of words in CSV column and writing to another CSV?

From Dev

counting specific characters in string

From Dev

counting specific file in directory

From Dev

Counting specific values in rows in CSV

From Dev

Counting occurence of a table of values

From Dev

Counting the occurence of substrings in matlab

From Dev

Counting the occurence of substrings in matlab

From Dev

Counting occurence of a table of values

From Dev

search for the last occurence of a string in a file and append to it

From Dev

Find every occurence of string in a file and store it in a List

From Dev

Delete the last occurence of a string in a file in unix/linux

From Dev

finding a specific split string in CSV file [Python]

From Dev

Search specific column for string input in a csv file

From Dev

Insert a string/number into a specific cell of a csv file

From Java

Subset string by counting specific characters

From Dev

Counting specific character from file

From Dev

Counting specific characters in a file (Python)

From Dev

Counting entries in a CSV file with python

From Dev

read from nth occurence of string till a specific string

From Dev

read from nth occurence of string till a specific string

From Dev

Counting occurence with a condition using awk

From Dev

Counting in R and preserving the order of occurence

From Dev

How to delete a part of a string in a cell after a second occurence of a specific character?

Related Related

  1. 1

    Counting occurence within a string Python

  2. 2

    PHP: Counting Extensions Occurence on a String

  3. 3

    Counting occurence of a word in a text file using R

  4. 4

    counting occurence of a word in a text file using python

  5. 5

    Counting occurrences of a string in a column of a csv file

  6. 6

    Targeting a specific occurence in a string and replacing it

  7. 7

    Counting occurence of words in CSV column and writing to another CSV?

  8. 8

    counting specific characters in string

  9. 9

    counting specific file in directory

  10. 10

    Counting specific values in rows in CSV

  11. 11

    Counting occurence of a table of values

  12. 12

    Counting the occurence of substrings in matlab

  13. 13

    Counting the occurence of substrings in matlab

  14. 14

    Counting occurence of a table of values

  15. 15

    search for the last occurence of a string in a file and append to it

  16. 16

    Find every occurence of string in a file and store it in a List

  17. 17

    Delete the last occurence of a string in a file in unix/linux

  18. 18

    finding a specific split string in CSV file [Python]

  19. 19

    Search specific column for string input in a csv file

  20. 20

    Insert a string/number into a specific cell of a csv file

  21. 21

    Subset string by counting specific characters

  22. 22

    Counting specific character from file

  23. 23

    Counting specific characters in a file (Python)

  24. 24

    Counting entries in a CSV file with python

  25. 25

    read from nth occurence of string till a specific string

  26. 26

    read from nth occurence of string till a specific string

  27. 27

    Counting occurence with a condition using awk

  28. 28

    Counting in R and preserving the order of occurence

  29. 29

    How to delete a part of a string in a cell after a second occurence of a specific character?

HotTag

Archive