Retrieve integers from a file

  • Python
  • Thread starter Jonas
  • Start date
  • #1
Jonas
6
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.
 

Answers and Replies

  • #2
41,301
18,940
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.
 
  • #3
Jonas
6
1
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).
 
  • #4
Ibix
Science Advisor
Insights Author
2022 Award
10,357
11,129
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>"
 
  • #5
Jonas
6
1
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.
 
  • #6
Ibix
Science Advisor
Insights Author
2022 Award
10,357
11,129
I have tried to split with commas and spaces
The code you posted doesn't do that. What did you try?
 
  • #7
Jonas
6
1
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)
 
  • #8
Ibix
Science Advisor
Insights Author
2022 Award
10,357
11,129
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?
 
  • #9
41,301
18,940
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.
 

Suggested for: Retrieve integers from a file

  • Last Post
Replies
3
Views
197
Replies
27
Views
512
  • Last Post
Replies
6
Views
527
Replies
8
Views
971
Replies
10
Views
370
Replies
8
Views
598
Replies
6
Views
5K
Replies
9
Views
773
Replies
2
Views
509
  • Last Post
Replies
5
Views
4K
Top