3

The data in the file looks like this; two columns that need to represent X and Y values. The first value in the will be X values and second value will be Y values.

   42.10      8.55
   40.25      7.60
   38.50      8.95
   39.55      6.45
   40.90      7.75

I need to read it into python and assign variables to each set of values in each line: for example, (42.10,8.55) where 42.10 is the x value and 8.55 is the y value. There are several lines of values that need this kind of organization. I can open the file and read it in however I can't get the variable assignments I want.

so far I have

with open ("file.txt", "r") as myfile:
    data=myfile.read().split()

Ok, so now here's where I'm at now:

with open('file.txt') as f:
    data = [line.split() for line in f.readlines()]
    out = [(float(x), float(y)) for x, y in data]
for i in out:
   print i

I want to create a scatter plot of the data in the for Loop, which module would you recommend?

crockeea
  • 21,651
  • 10
  • 48
  • 101
Astronerd326
  • 821
  • 2
  • 8
  • 12
  • 3
    show us what you have for opening the file and reading it in, and clarify what you mean for getting the variable assignments you want. – mhlester Mar 05 '14 at 23:28
  • with open ("file.txt", "r") as myfile: data=myfile.read().split() – Astronerd326 Mar 05 '14 at 23:32
  • 1
    [edit] your question with that. don't add it as a comment – mhlester Mar 05 '14 at 23:32
  • Is your data two numbers in a line like this: `1 2` (next line) `3 4` or a single line of numbers? When you say `The larger numbers will be X values and smaller numbers will be Y` does that mean the larger of the two numbers read? This is very unclear... – dawg Mar 05 '14 at 23:33
  • dawg, it's just like I have posted in the example above. The column beginning with 42.10 will be the X coordinates and the column beginning with 8.55 will be the Y coordinates. so the first position will be (42.10,8.55) – Astronerd326 Mar 05 '14 at 23:56
  • see my last comment below, there is a link to what you want, but you need to get the module matplotlib – Totem Mar 06 '14 at 00:48
  • ~I would make the plot question into a whole new question if I was you – Totem Mar 06 '14 at 00:49
  • 1
    I have matplotlib on my home PC so I'm set there. I looked at the link you shared and it seems very helpful, I'm just not familiar with the zip(*) call in the code so I'll do some research on that. If i run into trouble I'll create a new question for plotting it. – Astronerd326 Mar 06 '14 at 00:56

2 Answers2

3
data = []
for line in open(myfile).readlines():
       temp_x,temp_y = line.split()
       x = float(temp_x)
       y = float(temp_y)
       data.append((x,y))

You can get fancier, but this is all you need to get started.

The reason you only get the most recent values of x and y is because x and y change their values with each iteration of the loop. To see all of the values

for each_pair in data:
    (x, y) = each_pair
    print 'X = ',x, 'Y = ', y
PyNEwbie
  • 4,882
  • 4
  • 38
  • 86
  • I'm more concerned about the concept as opposed to "beauty" right now, thank you PyNEwbie. – Astronerd326 Mar 05 '14 at 23:41
  • alright I have a question to follow up the code. when i print x and print y it gives me the values for the first row of numbers. What do I need to do to have the remaining x and y values returned? – Astronerd326 Mar 05 '14 at 23:52
1

data and out are constructed here with list comprehensions:

with open('text.txt') as f:
    data = [line.split() for line in f.readlines()]
    out = [(float(k), float(v)) for k, v in data]

output:

for i in out:
    print i

(42.1, 8.55)
(40.25, 7.6)
(38.5, 8.95)
(39.55, 6.45)
(40.9, 7.75)

You can assign these to variables if you want, or you could reference them with out[0] etc.

How you go about naming them all depends on how many you have, but here is an example anyhow:

>>> first_tuple = out[0] 
>>> print first_tuple
(42.1, 8.55)
Totem
  • 7,189
  • 5
  • 39
  • 66
  • this is great, this is what I was working towards! I couldn't for the life of me get the two columns to pair rows. – Astronerd326 Mar 05 '14 at 23:59
  • Totem, now that I have Coordinates could I replace print i with plot i? Basically I want to create a scatter plot with the coordinates. – Astronerd326 Mar 06 '14 at 00:04
  • when you say plot, what do you mean exactly? Are you using any particular module etc for creating graphs? – Totem Mar 06 '14 at 00:16
  • as far as plotting goes I'd like to use numpy and pylab to create a scatter plot.And when I say plot I mean I have 50 sets of coordinates which you just helped me set up. So I'd want to plot a point at (42.1,8.55) another at (40.25,7.6)...etc as the set of coordinates in the "print i" appears – Astronerd326 Mar 06 '14 at 00:33
  • well yes, I would say that's very doable with the for loop instead of print. However, I am not so familiar with numbpy and pylab, so I would google that or search the site if I was you. Maybe cast another question specifically for this as it goes beyond the scope of your original question – Totem Mar 06 '14 at 00:35
  • Great thank you! So with the for loop constructed above I will use the plot procedure instead of print. do you have any recomendations for a module I could use to create a scatter plot with the data in the for loop? – Astronerd326 Mar 06 '14 at 00:40
  • I believe matplotlib is a module for graphs etc. I don't know exactly how it would work with a for loop to plot your graph, it might even be simpler than that. I would check that out. maybe start here: http://stackoverflow.com/questions/17478779/make-scatter-plot-from-set-of-points-in-tuples – Totem Mar 06 '14 at 00:45
  • When I return to my home computer I will give it a go with matplotlib. I'm very new to python and still trying to figure out which modules are best suited for particular situations. Thanks again Totem. – Astronerd326 Mar 06 '14 at 00:50