0

I'm trying to open a CSV file that contains 100 columns and 2 rows. I want to read the file and put the data in the first column into one list (my x_coordinates) and the data in the second column into another list (my y_coordinates)

X= []
Y = []

data = open("data.csv")
headers = data.readline()
readMyDocument = data.read()


for data in readMyDocument:
    X = readMyDocument[0]
    Y = readMyDocument[1]

print(X)
print(Y)

I'm looking to get two lists but instead the output is simply a list of 2's. Any suggestions on how I can change it/where my logic is wrong.

Dyland
  • 91
  • 1
  • 11
  • Can you include a small formatted sample of your `data.csv` file? – rahlf23 Apr 02 '19 at 01:26
  • I think you want something more like `readMyDocument = data.readlines()` (I assume `dataFile` is a typo because that variable doesn't exist). This will split on newlines. You're also overwriting the `X` and `Y` variables instead of appending to those lists, and you should be appending `data.split(",")[0]` and `[1]`; you're just repeatedly saving the first two characters of the file. Also, I assume `x_coordinates` and `y_coordinates` should just be `X` and `Y`. Don't forget to check that your example code runs =) – Nathan Apr 02 '19 at 01:27
  • 1
    Possible duplicate of: https://stackoverflow.com/questions/24662571/python-import-csv-to-list – R4444 Apr 02 '19 at 01:33

4 Answers4

0

You can do something like:

import csv

# No need to initilize your lists here
X = []
Y = []

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

X = data[0]
Y = data[1]

print(X)
print(Y)

See if that works.

R4444
  • 2,016
  • 2
  • 19
  • 30
0

You can use pandas:

import pandas as pd
XY = pd.read_csv(path_to_file)
X = XY.iloc[:,0]
Y = XY.iloc[:,1]

or you can

X=[]
Y=[]
with open(path_to_file) as f:
    for line in f:
        xy = line.strip().split(',')
        X.append(xy[0])
        Y.append(xy[1])
aerijman
  • 2,522
  • 1
  • 22
  • 32
0

First things first: you are not closing your file.
A good practice would be to use with when opening files so it can close even if the code breaks.

Then, if you want just one column, you can break your lines by the column separator and use just the column you want.

But this would be kind of learning only, in a real situation you may want to use a lib like built in csv or, even better, pandas.

X = []
Y = []

with open("data.csv") as data:
    lines = data.read().split('\n')
# headers is not being used in this spinet
headers = lines[0]
lines = lines[1:]

# changing variable name for better reading
for line in lines:
    X.append(line[0])
    Y.append(line[1])

print(X)
print(Y)

Ps.: I'm ignoring some variables that you used but were not declared in your code snipet. But they could be a problem too.

edinho
  • 406
  • 2
  • 6
0

Using numpy's genfromtxt , read the docs here: https://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html

Some assumptions:

  1. Delimiter is ","

  2. You don't want the headers obliviously in the lists, that's why skipping the headers.

You can read the docs and use other keywords as well.

import numpy as np

X= list(np.genfromtxt('data.csv',delimiter=",",skip_header=1)[:,0])
Y = list(np.genfromtxt('data.csv',delimiter=",",skip_header=1)[:,1])

Kartikeya Sharma
  • 1,335
  • 1
  • 10
  • 22