Python How can I retrieve integers from a file in Python?

  • Thread starter Thread starter Jonas
  • Start date Start date
  • Tags Tags
    File Integers
AI Thread Summary
The discussion centers around a user struggling to extract integers from two formatted text files containing 10,000 integers each, with one file including negative numbers. The user attempts various methods to convert the data into usable integers or floats but encounters errors. The initial code only removes whitespace and commas, failing to split the lines into individual numbers. Participants emphasize the importance of understanding how the numbers are separated within the lines, suggesting that the user needs to split the lines correctly based on the actual delimiters used (commas or colons). The user shares attempts to split the data but continues to face issues, indicating a misunderstanding of how to append the correct values to the list. The conversation highlights the need for proper data parsing techniques in Python to achieve the desired results.
Jonas
Messages
6
Reaction score
1
TL;DR Summary
Trying to retrieve integers from two separeate files with different conditions.
Hey.

Im pretty new to Python (and programming in general in fact). I have received two different files, each containing 10 000 integers mixed up with some commas, colons, and \n, and in one of the files there are also negative numbers. I have tried all day retrieving those numbers using all the different techniques i have found online, in the books etc to be able to finish the excercise. But nothing seems to work, i can't convert them to floats or integers no matter what i do. The files are formatted in such way that it's 10 numbers ( in one of them it's ranging from -999 to 999, and in the other from 0 to 999) per row and then a line-break.
Code:
with open(numbers_A) as f:
    for line in f:
        line = line.strip()
        a = line.replace(',','')
        lista.append(a)
    print(len(lista))

This gives the result 1000 (length of list).
If i try to convert a to int or float i get an error. So i interpret that as one line from the file => one entry in the list. Whereas i would like to think that there are 10 000 entries - one for each value. The task after this is trivial, calculate mean and standard dev. If anyone have an idea i would be thankful.
 
Technology news on Phys.org
Jonas said:
i interpret that as one line from the file => one entry in the list

Yes, that's correct. All your code is doing is stripping whitespace from each line and removing commas from each line. It isn't doing anything to split each line into individual numbers.

In order to split each line into individual numbers, you have to know how the numbers are separated in each line. Are they separated by spaces? Tabs? Commas? (Hopefully not commas since then your removing the commas would remove any ability to separate the numbers.) Something else? Without knowing that it's impossible to know how to extract 10 numbers from each line.
 
PeterDonis said:
Yes, that's correct. All your code is doing is stripping whitespace from each line and removing commas from each line. It isn't doing anything to split each line into individual numbers.

In order to split each line into individual numbers, you have to know how the numbers are separated in each line. Are they separated by spaces? Tabs? Commas? (Hopefully not commas since then your removing the commas would remove any ability to separate the numbers.) Something else? Without knowing that it's impossible to know how to extract 10 numbers from each line.

It's a text file and the first looks like:
Code:
237, 321, 212, 379, 201, 247, 201, 204, 298, 130
285, 229, 167, 235, 353, 176, 130, 274, 337, 192
367, 88, 235, 275, 290, 221, 141, 148, 341, 365
.
.
. and so on for 1000 rows

And the second one is separated only by ':' (and all in one line it seems).
 
PeterDonis said:
split
A bit of judicious googling may well help you, @Jonas. I always tell my students that it's amazing how far you can get with googling "<name of language> how do I <do whatever>"
 
Ibix said:
A bit of judicious googling may well help you, @Jonas. I always tell my students that it's amazing how far you can get with googling "<name of language> how do I <do whatever>"

I have tried to split with commas and spaces, but it doesn't work either. Nothing changes, it still reads line by line.
 
Jonas said:
I have tried to split with commas and spaces
The code you posted doesn't do that. What did you try?
 
Ibix said:
The code you posted doesn't do that. What did you try?
No i have tried many different ways. Among others this one:

Code:
with open(numbers_A) as f:
    for i in f:
        i = i.strip('\n')
        i = i.split(', ')
        for x in range(len(i)):
            lista.append(x)
    print(lista)
 
Jonas said:
No i have tried many different ways. Among others this one:

Code:
with open(numbers_A) as f:
    for i in f:
        i = i.strip('\n')
        i = i.split(', ')
        for x in range(len(i)):
            lista.append(x)
    print(lista)
That's closer. What values would you expect x to take in the loop?
 
Ibix said:
That's closer.

To be clear, "closer" is not the same as "correct". The answer to the question you ask should help the OP to see why.
 
Back
Top