1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
spin [16.1K]
3 years ago
5

Define the body of the function read_data(filename). If filename is None, return the sentence (already defined for your), otherw

ise return the contents of the file (whose name is in the parameter filename). If there is any error during the opening or reading of the file, print out a warning and return sentence. If you haven't completed the lesson on Python local I/O, do so now.
Computers and Technology
1 answer:
Morgarella [4.7K]3 years ago
8 0

Answer:

Answer explained

Explanation:

The main difference between parse_text_into_words( v1 and v2) is I have used splitlines also in v1 so the escape sequence '\n' will not be present in the list of words. while in v2 only split function is used so it will have '\n' present in words list.

Code:

def read_file(fileName = None):

  try:

      File = open(fileName,"r") # opening file in read mode

      for row in File:          # for each line in File

          print(row,end="")

  except IOError:      # handling exception of file opening

      print("Could not read file:",fileName)

def parse_text_into_words_v1(text):

  list_words = []

  list_lines = text.splitlines()

  for i in list_lines:

      list_words.extend(i.split(' '))

  return list_words

def parse_text_into_words_v2(text):

  list_words = []

  list_words.extend(text.split(' '))

  return list_words

def determine_difference(a_list,b_list):

  print("first list:",a_list)

  print("second list:",b_list)

  a_set = set(a_list)

  b_set = set(b_list)

  diff = a_set - b_set

  diff = list(diff)

  print("\nDifference between 2 sets: ",diff)

if __name__ == '__main__':

  print("Reading file using function read_file() to read \"data.txt\" :")

  read_file("data.txt")

  print("\n\n")

  t = '''vhfhyh ghgggj ghchvjhvj'''

  print("\nDemonstrating implementation of parse_text_into_words_v1:")

  l = parse_text_into_words_v1(t) # calling function with text parameter

  print("list of words:")

  print(l)

  print("\nDemonstrating implementation of parse_text_into_words_v2:")

  b = parse_text_into_words_v2(t) # calling function with text parameter

  print("list of words:")

  print(b)

  print("\nDemonstrating difference between two lists")

  a_list = [1,2,3,4,5,6,7,8,9,10]

  b_list = [2,4,6,8,10]

  determine_difference(a_list,b_list) # passing two list to take difference

You might be interested in
Join me to play mm2<br><br><br>in r0bl0x​
jenyasd209 [6]
Ok what's the code i will join
4 0
3 years ago
Read 2 more answers
Critics of media consumption
Mumz [18]

Answer:

b

Explanation:

make me brainlyest plzz

sorry if i got it wrong

5 0
3 years ago
Read 2 more answers
Cine stie repede va rog mult si repede
Nostrana [21]

Answer:

can't understand the language

4 0
3 years ago
What type of file is MyFile.dll?
pav-90 [236]

Answer:

data file

Explanation:

DLL, in full dynamic link library, a file containing code for commonly used program functions on PCs

3 0
4 years ago
Read 2 more answers
DRIVER ED
julsineya [31]

This is true. Motorcycles are much smaller than cars and therefore more easily can slip into a blind spot. Motorcyclists also have less protection than other drivers and would likely suffer more injury in a collision.

4 0
4 years ago
Read 2 more answers
Other questions:
  • Shut down and unplug the computer. Remove the CPU case lid. Locate the various fans, and then use compressed air to blow dirt ou
    13·2 answers
  • When an object is selected, it is surrounded by ________, which allow you to change the size of the object.?
    5·1 answer
  • Which LOOKUP function will you use if you want to search for a value in the columns?
    5·1 answer
  • Will sum1 come bust me outta FCA....replies u gotta live in knoxville though
    13·2 answers
  • Why you think Operating System is pivotal in teaching and learning.
    13·1 answer
  • What term best describes when the scope of the project changes from what was initially agreed upon?
    8·1 answer
  • To use a macro, a user must first <br> ____ a macro and then <br> ____the macro.
    8·1 answer
  • PLSSS HELP!! During the late 20th century, immigration to the United States increased dramatically from
    7·1 answer
  • Explain four sitting postures in a computer laboratory​
    9·1 answer
  • To freeze rows 1 and 2, and columns 1, 2, and 3, which cell should you highlight before selecting freeze panes?.
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!