Any plain text file in English, counting the number of words that appear in the language (Shell Python two languages are implemented)

Source: Internet
Author: User

The existing plain text titled Test.txt, which counts the number of occurrences of the word.

Content of Test.txt:

I have the application Someday Oneday day Demo

I have some one coma ideal naive I


Code implemented in Python:

Import re

Count = {}

f = open (' Test ', ' R ')

b = F.read ()

#print b

cd = Re.split (' [\\n]+ ', b) #注意split的用法

Print CD


For I in CD:

Count[i] = Count.get (i,0) + # Note the use of the Get () method

Print Count


Results obtained after executing the code:

[' I ', ' have ', ' has ', ' application ', ' Someday ', ' oneday ', ' Day ', ' demo ', ' I ', ' has ', ' some ', ' one ', ' coma ', ' ideal ', ' Nai ve ', ' I ']

{' Someday ': 1, ' I ': 3, ' demo ': 1, ' naive ': 1, ' some ': 1, ' One ': 1, ' Application ': 1, ' ideal ': 1, ' has ': 3, ' coma ': 1, ' on Eday ': 1, ' Day ': 1}


The shell implements the method as follows:

TR "" "\\n"

Run the result as


1 application

1 coma

1 day

1 Demo

3 have

5/

1 Ideal

1 naive

1 One

1 oneday

1 some

1 Someday






This article is from "Jason's blog" blog, please be sure to keep this source http://jason83.blog.51cto.com/12723827/1982168

Any plain text file in English, counting the number of words that appear in the language (Shell Python two languages are implemented)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.