How can I retrieve integers from a file in Python?

In summary, the conversation is discussing difficulties in retrieving numbers from a file using different techniques in Python. The code provided is not properly splitting each line into individual numbers, and the conversation suggests trying different methods such as using spaces or commas to separate the numbers.
  • #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.
 
Technology news on Phys.org
  • #2
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.
 
  • #3
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).
 
  • #4
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>"
 
  • #5
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.
 
  • #6
Jonas said:
I have tried to split with commas and spaces
The code you posted doesn't do that. What did you try?
 
  • #7
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)
 
  • #8
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?
 
  • #9
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.
 

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

What does it mean to "retrieve integers from a file"?

Retrieving integers from a file means to access and extract numerical data stored in a file in order to use it for analysis or manipulation.

What file formats can be used to store integers?

Integers can be stored in a variety of file formats, including text files, CSV files, JSON files, and binary files.

How can I retrieve integers from a file in a specific programming language?

The method for retrieving integers from a file will vary depending on the programming language you are using. However, most languages have built-in functions or libraries that can be used to read and extract data from files.

What are some potential challenges when retrieving integers from a file?

One potential challenge when retrieving integers from a file is ensuring that the data is formatted correctly and can be easily parsed. Another challenge could be handling errors or exceptions if the file is corrupted or does not contain the expected data.

Are there any best practices for retrieving integers from a file?

Yes, some best practices for retrieving integers from a file include properly formatting the data, handling errors and exceptions, and efficiently parsing the data to avoid any performance issues.

Similar threads

Replies
4
Views
4K
Replies
8
Views
1K
Replies
16
Views
3K
Replies
18
Views
1K
Replies
4
Views
6K
Replies
9
Views
2K
Back
Top