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
  Help with reading json file hhchenfx 5 284 Jul-07-2021, 01:58 PM
Last Post: hhchenfx
  [Solved] Reading every nth line into a column from txt file Laplace12 7 466 Jun-29-2021, 09:17 AM
Last Post: Laplace12
  help with project of reading and searching big log file korenron 6 265 Jun-24-2021, 01:57 PM
Last Post: korenron
  Helps with reading csv file - 3 methods hhchenfx 4 478 May-13-2021, 04:15 AM
Last Post: buran
  Subprocess.Popen() not working when reading file path from csv file herwin 13 1,132 May-07-2021, 03:26 PM
Last Post: herwin
  find the header location in a .bin file without reading the whole file at a time SANJIB 0 471 Mar-05-2021, 04:08 PM
Last Post: SANJIB
  Reading a csv file Led_Zeppelin 2 646 Feb-26-2021, 05:48 AM
Last Post: buran
  reading a csv file Led_Zeppelin 3 839 Feb-19-2021, 02:16 PM
Last Post: Led_Zeppelin
  Reading from a file. krhoades 3 639 Dec-03-2020, 09:52 PM
Last Post: krhoades
  reading json file DrBrownStats 1 502 Nov-25-2020, 09:00 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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