0

Content of file scores.txt that lists the performance of players at a certain game:

80,55,16,26,37,62,49,13,28,56
43,45,47,63,43,65,10,52,30,18
63,71,69,24,54,29,79,83,38,56
46,42,39,14,47,40,72,43,57,47
61,49,65,31,79,62,9,90,65,44
10,28,16,6,61,72,78,55,54,48

The following program reads the file and stores the scores into a list

f = open('scores.txt','r')
L = []
for line in f:
    L = L + map(float,str.split(line[:-1],','))
print(L)

But it leads to error messages. I was given code in class so quite confused as very new to Pyton. Can I fix code?

2
  • 2
    "leads to error messages" Did you feel like sharing them with us? Commented Oct 21, 2017 at 4:49
  • 2
    you should be using with open('scores.txt', 'r') Commented Oct 21, 2017 at 4:51

1 Answer 1

7

It appears you've adapted python2.x code to use in python3.x. Note that map does not return a list in python3.x, it returns a generator map object (not a list, basically) that you've to convert to a list appropriately.

Furthermore, I'd recommend using list.extend instead of adding the two together. Why? The former creates a new list object every time you perform addition, and is wasteful in terms of time and space.

numbers = []
for line in f:
    numbers.extend(list(map(float, line.rstrip().split(','))))

print(numbers)

An alternative way of doing this would be:

for line in f:
    numbers.extend([float(x) for x in line.rstrip().split(',')]) 

Which happens to be slightly more readable. You could also choose to get rid of the outer for loop using a nested list comprehension.

numbers = [float(x) for line in f for x in line.rstrip().split(',')]

Also, forgot to mention this (thanks to chris in the comments), but you really should be using a context manager to handle file I/O.

with open('scores.txt', 'r') as f:
    ...

It's cleaner, because it closes your files automatically when you're done with them.


After seeing your ValueError message, it's clear there's issues with your data (invalid characters, etc). Let's try something a little more aggressive.

numbers = []
with open('scores.txt', 'r') as f:
    for line in f:
        for x in line.strip().split(','):
            try:
                numbers.append(float(x.strip()))
            except ValueError:
                pass

If even that doesn't work, perhaps, something even more aggressive with regex might do it:

import re

numbers = []
with open('scores.txt', 'r') as f:
    for line in f:
        line = re.sub('[^\d\s,.+-]', '', line)
        ... # the rest remains the same
Sign up to request clarification or add additional context in comments.

6 Comments

@Coldspeed: thank you for your reply. I tried your codes but all of them yield the same error "ValueError: could not convert string to float:". I would start with "with open('scores.txt', 'r') as f:" and then write, e.g., your first code suggestion. What am I doing wrong?
@Lola Problem with your data - the format of your text file, and the code that processes it are not in sync. This answer assumes you know what you are doing when you read your file. If you don't know how to read your file, then you have an entirely different problem.
@Coldspeed: Then, I don't know what I am doing when I read the file. I was given the code in the class section dedicated exactly to reading files. I thought, your code was exactly meant to have an object "numbers" standing for the data. have I got it wrong? Can you please help me out? All I need is to be able to read text files into Python.
@Lola Okay. There are problems with your data, so I've added a method to handle errors. If it works, don't forget to vote and accept. Thanks.
@Lola x = line.rstrip() removes trailing whitespace. y = x.split(',') generates a list. z = map(float, y) will apply the float function to each element in y. k = list(z) will convert the resultant map object to a list. numbers.extend(k) will then add all the elements in k to numbers. Unfortunately, that didn't work because it is a fragile piece of code that cannot handle invalid elements in y. For example, float('blah blah') is an invalid operation, which this line can't pick up on.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.