Python Forum
Code not reading http link from .txt file (Beginner level)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code not reading http link from .txt file (Beginner level)
#1
Hello fellow users,

I have a doubt regarding a code I wrote which reads .txt files. This was a practice exercise I found on the net which prompts the student to write a code capable to read a .txt file and extract all the numbers in it. Later these numbers should be summed up and return the total value. This is what I got so far:
file = input("Enter the name of the file to read: ")
text = open(file)

import re
numbers = list()
counter = 0

for lines in text:
    words = lines.split()
    for word in words:
        num = re.findall('[0-9]+', word)
        if len(num) != 1: continue
        for i in num:
            ii = int(i)
            numbers.append(ii)
            counter = counter+1


print("There are ",counter, "numbers")
print("an this is the list of them:")
print(numbers) 
print("The total sum of the numbers is: ",sum(numbers))  
(The text to be read is the following:)

Output:
The even better news is that I already came up with a simple program to find the 342 most common word in a text file. I wrote it, tested it, and now I am giving it to you to use so you can save some time. You don't even need to know Python to use this program. You will need to get through 34 Chapter 44ten of this book to fully understand the awesome Python techniques that were used to make the program. You are22 the end user, you simply use the program and marvel at its cleverness and how it saved you so much 333 manual effort. You simply type the code into a file called words.py and run it or you download the source code from http://www.py4e.com/code3/ and run it. www.py8e.com 456 because 34 595.
Okay, it seems the code is able to find numbers and sum them up. But I've noticed that the result (9 numbers = 1868 total value) doesn't match the actual right solution (should be 10 numbers = 1875 total value). There's a deviation of 7, which I concluded is coming from the code not detecting the numbers in "http://www.py4e.com/code3/". However, the code IS able to detect the number in the other line "www.py8e.com".

So basically, it seems as the code is in trouble when it comes to a string with "http" on it, but don't understand why and how can I solve it.

Any ideas from you guys will be much appreciated.
Thanks :)
Larz60+ write Dec-13-2020, 02:05 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Pleas use code tags on future posts.
Reply
#2
What is the purpose of line 12? Your problem has nothing to do with http. That just happens to be the only place in the text where you have two different numbers in the same "word".
Reply
#3
(Dec-13-2020, 06:56 AM)bowlofred Wrote: What is the purpose of line 12? Your problem has nothing to do with http. That just happens to be the only place in the text where you have two different numbers in the same "word".

Hey bowlofred thanks for your reply.
I typed line 12 to avoid the "for" be actioned for empty lines. But I guess this time it's useless.
Regarding your suggestion, I'm aligned with you that the code is only getting the 1st number in the word. How could I make the code read ALL the numbers (even if two are in the same word).

Cheers
Reply
#4
Just get rid of line 12. If it finds more than one number in the word, the continue on line 12 skips them.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad problems with reading csv file. MassiJames 3 559 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Reading a file name fron a folder on my desktop Fiona 4 851 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Beginner: Code not work when longer list raiviscoding 2 764 May-19-2023, 11:19 AM
Last Post: deanhystad
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Reading a file JonWayn 3 1,058 Dec-30-2022, 10:18 AM
Last Post: ibreeden
  Review my code: convert a HTTP date header to a datetime object stevendaprano 1 1,905 Dec-17-2022, 12:24 AM
Last Post: snippsat
  Reading Specific Rows In a CSV File finndude 3 940 Dec-13-2022, 03:19 PM
Last Post: finndude
  Excel file reading problem max70990 1 865 Dec-11-2022, 07:00 PM
Last Post: deanhystad
  Replace columns indexes reading a XSLX file Larry1888 2 951 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  Failing reading a file and cannot exit it... tester_V 8 1,755 Aug-19-2022, 10:27 PM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020