1

I am trying to read a line of numbers in a csv file, then call on them individually to compute with them. Here is my code so far:

import sys
import os
import csv
import numpy as np

with open('data.csv') as csv_file:
    csv_reader = csv.reader(csv_file)

    for line in csv_reader:

        x = np.array(line[3])
        print(x)

Within this line of the csv file there are numbers with decimals like 4.65 that I need to use in the calculations. I tried things like:

print(5 + x[14]) but it won't work.

I assume I need to convert the line into a list of integers or something, please help.

Thank you in advance.

9
  • Do you really need a numpy array or do regular arrays suffice? Commented Oct 24, 2017 at 23:02
  • either one should suffice Commented Oct 24, 2017 at 23:03
  • Have you tried numpy.loadtxt or numpy.genfromtxt? Commented Oct 24, 2017 at 23:07
  • Welcome to SO. Please take the time to read How to Ask. float()' Commented Oct 24, 2017 at 23:07
  • 1
    Can you give us an example of a line as it is presented in the csv ? Commented Oct 24, 2017 at 23:09

2 Answers 2

1

According to your example line you want to add delimiter=' ' to the csv.reader()

csv_data = csv.reader(csv_file, delimiter=' ')

Taking a guess at the structure of your csv, but under the assumption that it contains no headings and you want to keep the decimals:

with open('new_data.txt') as csv_file:
    csv_data = csv.reader(csv_file, delimiter=' ')
        for line in csv_data:
            x = [float(n) for n in line.split(',')]
            print(x)

This will fail if you have string values, such as 'A string'

Sign up to request clarification or add additional context in comments.

8 Comments

it returned: ValueError: could not convert string to float: '5.11,0.5,5.61'
those are the first numbers in the string
Can you give me a sample of the csv file?
['5.11' '0.5' '5.61'] ['5.08' '0.5' '5.58'] ['5.06' '0.5' '5.56'] ['4.74' '-0.5' '4.24'] ['' '' ''] ['' '' ''] ['6.486' '1' '7.486'] ['4.687' '-0.5' '4.187'] ['4.488' '-0.5' '3.988'] ['4.34' '-0.5' '3.84']
There are no commas which confuses me, try line.split(',') in the list comprehension of my answer.
|
1

Here is an alternative to @GiantsLoveDeathMetal's solution with map (also it shows a way to provide us a copy/paste-able piece of code containing a sample of your csv file with io.StringIO) :

EDITED the StringIO to contain data in columns and with empty rows

import csv
import io 

f = io.StringIO("""
7.057
7.029

5.843
5.557
4.186
4.1

2.286""")
csv_reader = csv.reader(f, delimiter=' ')
for line in csv_reader:
    line = list(map(float, filter(None, line)))
    print(line)

In python 2 (and in some cases in python 3 if your code works with a generator, which is not the case in this example), you don't need to convert the result of map to a list.

So line = list(map(float, line)) can be replaced by the much cleaner map(float, line). Which can be considered cleaner and more explicit than a list comprehension (or a generator expression).

For instance this will work :

import csv
import io 

f = io.StringIO("""7.057 7.029 5.843 5.557 4.186 4.1 2.286""")
csv_reader = csv.reader(f, delimiter=' ')
for line in csv_reader:
    line = map(float, line)
    print(sum(line))
# 36.05800000000001

If you're interested in the map vs list comprehension debate, here you go for more details.

9 Comments

Nice map. Please get rid of unused imports it will make your answer look less like a beast. :)
thanks Unatiel, however I this solution only works if I keep the string short enough so that its on one line. Also, if I write out the string then there is no point in haveing the csv file, but i need to be calling on the numbers from that file to do computations.
@ChristopherRivas The StringIO is used to provide a file-like object for csv.reader. It's a dummy file in memory for demonstration purposes. For your real code, you would of course use open() on your file. What we need now is an actual sample of your csv file so we can use the right parameters for csv.reader. If the line you've provided in @GiantsLoveDeathMetal's answer is indeed how your csv looks like, then a confirmation would be welcome. Because you've provided a different sample in the comment to your question. Also, a StringIO can indeed contain multiple lines.
my file is an exel file. 5.61 5.58 5.56 4.24 7.486 4.187 3.988 3.84 1.057 1.029 1.843 5.7 9.86 4.1 7.26 7.95 3.071 7.98 8.56 4.36 1.63 1.67 1.875 4.683 1.9 1.4
sorry, here is what it looks like: 5.61 5.58 5.56 4.24 7.486 4.187 3.988 3.84 1.057 1.029 1.843 5.7 9.86 4.1 7.26 7.95 3.071 7.98 8.56 4.36 1.63 1.67 1.875 4.683 1.9 1.4
|

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.