Firstly, note that you are not closing the file after opening it. A Better way to read from a file would be to use the with
statement. Read What is the python "with" statement designed for? What is the python "with" statement designed for? to know more about the with
statement.
Code Golfing the solution, We can use a List Comprehension. Read this this to kow more about the list comprehensions.
Firstly, note that you are not closing the file after opening it. A Better way to read from a file would be to use the with
statement. Read What is the python "with" statement designed for? to know more about the with
statement.
Code Golfing the solution, We can use a List Comprehension. Read this to kow more about the list comprehensions.
Firstly, note that you are not closing the file after opening it. A Better way to read from a file would be to use the with
statement. Read What is the python "with" statement designed for? to know more about the with
statement.
Code Golfing the solution, We can use a List Comprehension. Read this to kow more about the list comprehensions.
Firstly, note that you are not closing the file after opening it. A Better way to read from a file would be to use the with
statement. Read What is the python "with" statement designed for? to know more about the with
statement.
The code would be,
with open("data.csv") as filename:
# Code Follows
Other things to note here are
- The mode of the
open
statement is'r'
by default and hence, you don't need to explicitly mention it. - Use a meaningful variable name like
filename
. This is to help your future self from not asking What is thef
?
Now coming to the core of the problem. You can use the fileobject directly to iterate over the lines, You do not need to read the entire file into a list. This is helpful for large files. The code will be
for line in filename:
# Code follows
The line line2 = line.replace("\n", "")
is quite inadequate as there might be other whitespace like a trailing space or a \r
. To solve all these issues, you can use str.strip
.
stripped_line = line.strip()
Again, Note the name of the variable. Coming to this code block
line2 = line2.split(",")
Nlines = []
for i in line2:
i = float(i)
Nlines.append(i)
sums = sum(Nlines)
You can solve this issue using split
and a generator expression.
stripped_line = stripped_line .split(',')
line_sum = sum(float(i) for i in stripped_line)
Alternatively you can use map
line_sum = sum(map(float,stripped_line))
(You can use literal_eval
to directly do sum(literal_eval(stripped_line))
, But the string functions are the right (and preffered way))
Hence the complete code will be reduced to (excluding the data creation part)
def line_averages():
with open("test.csv", "r") as filename:
avLines= []
for line in filename:
stripped_line = line.strip()
stripped_line = stripped_line.split(',')
line_sum = sum(float(i) for i in stripped_line )
avLines.append(line_sum/len(stripped_line ))
return avLines
Code Golfing the solution, We can use a List Comprehension. Read this to kow more about the list comprehensions.
def line_averages():
with open("test.csv", "r") as filename:
lines = [[float(i) for i in line.strip().split(',')] for line in filename]
return [sum(i)/len(i) for i in lines]