Jarred Parr
Jarred Parr

Reputation: 1255

Store data based on location in a dataset python

Forgive me if this question is trivial, I am just having some trouble finding a solution online, and I'm a bit new to python. Essentially, I have a dataset which is full of various numbers all of which are arranged in this format:

6.1101,17.592
5.5277,9.1302
8.5186,13.662

I'm trying to write some python to get the number on either side of the comma. I assume it's some type of splitting, but I can't seem to find anything that works for this problem specifically since I want to take the ALL the numbers from the left and store them in a variable, then take ALL the numbers on the right store them in a variable. The goal is to plot the data points, and normally I would modify the data set, but it's a challenge problem so I am trying to figure this out with the data as is.

Upvotes: 2

Views: 238

Answers (1)

brennan
brennan

Reputation: 3493

Here's one way:

with open('mydata.csv') as f:
    lines = f.read().splitlines()

left_numbers, right_numbers = [], []

for line in lines:
    numbers = line.split(',')
    left_num = float(numbers[0])
    right_num = float(numbers[1])
    left_numbers.append(left_num)
    right_numbers.append(right_num)

Edit: added float conversion

Upvotes: 1

Related Questions