Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python counts the number of words in the text and two ways of writing print

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

#! / usr/bin/python#-*-coding: utf-8-*-# function, counting the number of words in each text separately, and outputting the total number of words in all text a = [] sum = 0def count_words (filename): # filename = '1.txt' try: with open (filename) as file_object: t = file_object.read () except IOError: print' you have' +'+ filename +'is not words Else: words = t.split () numbers = len (words) a.append (numbers) # print can be written with a comma followed directly by the parameter value. It can also be written without a comma, followed by the% parameter value print 'danci de geshi yigong shi% d'% numbers# # print' danci de geshi yigong shi', numbersfilenames = ['1.txtcounting, numbersfilenames = [1.txtcounting, 2.txtrecording, 3.txt'] for filename in filenames: count_words (filename) for i in a: sum + = int (I) print sum#, one is counted in sum, the other is in sum function, and the parameter of # sum is a list. Here an is list#print 'the total number of all words is' + str (sum (a))

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report