Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

is it return error? 12.1 (2 pts) In Section 4.3, pages 112-114 discuss four ways of reading text from a file. The first way to

is it return error? image text in transcribed
image text in transcribed
image text in transcribed
12.1 (2 pts) In Section 4.3, pages 112-114 discuss four ways of reading text from a file. The first way to read text from a file is shown in the listing at the bottom of page 112. On line 4 of that listing we see: content = infile.read() After this line of code is executed, what would type (content) return? 12.2 (5 pts) The second way to read text from a file is shown in the middle of p. 113. On line 7 of that listing we see: wordList - content.split() What does the .split() method do, and what is stored in wordList as a result? One way to access the text file content is to read the content of the file into a string object. This pattern is useful when the file is not too large and string operations will be used to process the file content. For example, this pattern can be used to search the file content or to replace every occurrence of a substring with another We illustrate this pattern by implementing function numChars(), which takes the name of a file as input and returns the number of characters in the file. We use the read() function to read the file content into a string: t.py def numChars (filename): returns the number of characters in file filename infile = open(filename, 'r') content - infile.read() infile.close() return len(content) The file reading pattern we discuss next is useful when we need to process the words of a file. To access the words of a file, we can read the file content into a string and use the string split() function, in its default form, to split the content into a list of words. (So, our definition of a word in this example is just a contiguous sequence of nonblank characters.) We illustrate this pattern on the next function, which returns the number of words in a file. It also prints the list of words, so we can see the list of words. Module: text.py def numWords (filename): returns the number of words in file filename! infile = open(filename, '') content - infile.read() #read the file into a string infile.close() wordList - content.split() split file into list of words # print list of words too print (wordList) return len (wordList) i n our example tile

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Vi Special Issue On Database And Expert Systems Applications Lncs 7600

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2012th Edition

3642341780, 978-3642341786

More Books

Students also viewed these Databases questions

Question

The amount of work I am asked to do is reasonable.

Answered: 1 week ago

Question

The company encourages a balance between work and personal life.

Answered: 1 week ago