Phone

+919997782184

Email

support@roboticswithpython.com

Geeks of Coding

Join us on Telegram

Home Forums Assignment courserra Python for Everybody – Specialization Using Python to access Web data Assignment 2 Extracting Data With Regular Expressions

Viewing 0 reply threads
  • Author
    Posts
    • #884
      Abhishek TyagiAbhishek Tyagi
      Keymaster

      Finding Numbers in a Haystack
      In this assignment, you will read through and parse a file with text and numbers. You will extract all the numbers in the file and compute the sum of the numbers.
      Data Files
      We provide two files for this assignment. One is a sample file where we give you the sum for your testing and the other is the actual data you need to process for the assignment.
      Sample data: http://py4e-data.dr-chuck.net/regex_sum_42.txt (There are 90 values with a sum=445833)
      Actual data: http://py4e-data.dr-chuck.net/regex_sum_97406.txt (There are 67 values and the sum ends with 785)
      These links open in a new window. Make sure to save the file into the same folder as you will be writing your Python program. Note: Each student will have a distinct data file for the assignment – so only use your own data file for analysis.

      #copy all content from "http://py4e-data.dr-chuck.net/regex_sum_97406.txt" file and create text file then run following code

      import re
      
      sum = 0
      
      file = open('regex_sum_97406', 'r')
      for line in file:
          numbers = re.findall('[0-9]+', line)
          if not numbers:
              continue
          else:
              for number in numbers:
                  sum += int(number)
      
      print(sum)
Viewing 0 reply threads
  • You must be logged in to reply to this topic.