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.

viddik13
  • 5,930
  • 2
  • 14
  • 21

2 Answers2

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'

foxyblue
  • 2,859
  • 2
  • 21
  • 29
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.

Unatiel
  • 1,060
  • 1
  • 11
  • 17
  • Nice `map`. Please get rid of unused `import`s it will make your answer look less like a beast. :) – foxyblue Oct 25 '17 at 00:50
  • 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. – Christopher Rivas Oct 25 '17 at 02:44
  • @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. – Unatiel Oct 25 '17 at 02:54
  • 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 – Christopher Rivas Oct 25 '17 at 03:29
  • 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 – Christopher Rivas Oct 25 '17 at 03:30
  • its all these numbers in a column of cells – Christopher Rivas Oct 25 '17 at 03:31
  • Ah, I think I got it. I guess you're copying and pasting directly from excel ? If I'm correct, then you should open your csv with the notepad or whatever text editor, and show us this. – Unatiel Oct 25 '17 at 03:33
  • when i open the exel file in my TextEdit application, it looks like what I most recently commented exept its straight down, and there's a vertial space every 4 or 5 numbers because thats where in the exel file there are empty cells. – Christopher Rivas Oct 25 '17 at 03:41
  • So, no commas or anything other than digits, dots and spaces in the file ? – Unatiel Oct 25 '17 at 03:46