0

I need to import a csv file, which contains about 1.000.000 numbers. they are all separated with decimals (,) so: 1,2,3,4,...

they just ordered easily in the file: no blanks no paragraphs.

This is my actual code:

import statistics
import csv

with open('test.csv', 'r') as f:
    reader = csv.reader(f)
    zahlenliste = list(reader)

print(zahlenliste)

# x = statistics.mean(zahlenliste)
# print(x)

I tried many codes, as on stackoverflow was presented, but I just couldn't execute it without any error.

with all the numbers in the list, i want the arithmetic mean as a result (which is actutally in commentary). with the print(zahlenliste) i wanted to look what the content of the list actually is and looks like:

[['1', '2', '3', '4', '5']]

would you be kind and help me just adding the right function to import the numbers as float to use it in the arithmetic.mean function?

4
  • It is much easier for us to see what is going wrong if you include the actual error message. Commented Jun 13, 2016 at 12:08
  • 2
    Welcome to SO :-) Please look at how to ask Commented Jun 13, 2016 at 12:08
  • Thanks for the edit Ulrich, @JETM: i know that it would be better, but as i am a beginner at importing files, i just tried few code snippets and tried to change them to my own code. so i just got a few different errors. Commented Jun 13, 2016 at 12:26
  • as i execute my actual code i get: File "...\wa\prog\listezahlen.py", line 11, in <module> x = statistics.mean(zahlenliste) File "...\\Python35-32\lib\statistics.py", line 293, in mean return sum(data)/n File "...\\lib\statistics.py", line 162, in _sum n, d = exact_ratio(x) File "...\\lib\statistics.py", line 218, in _exact_ratio raise TypeError(msg.format(type(x).__name_)) from None TypeError: can't convert type 'list' to numerator/denominator Commented Jun 13, 2016 at 12:27

1 Answer 1

2

The items in your zahlenliste are characters, you'll need to cast them to numbers (int or float). A list comprehension comes in handy here:

zahlenliste = [[int(item) for item in line] for line in reader]

Now you've got a list of lists: The inner lists contain a line's values, the outer list represent the lines. If you need to calculate the mean value for each line, use another list comprehension:

mittelwerte = [statistics.mean(line) for line in zahlenliste]
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but i get this error: float() argument must be a string, a bytes-like object or a number, not 'list'
Of course, forgive me, I missed the nested statement. Should work by now
it works! great. but unfortunately i cant work further with the result. after using the arithmetic.mean it shows this error again: "can't convert type 'list' to numerator/denominator"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.