2

I am having some trouble attempting to manipulate a CSV file and appending the results to a new column.

Essentially I have a csv file (delimited ;) with 5 columns currently (of Cartesian coords [X, Y] and components [dX, dY], and magnitude/ length). I wish to add the result of some equations, which differ depending on the value of my Cartesian components, to a 6th column in this csv file (the angle).

Thus far my code is this (the maths is correct [hopefully], it's just the appending that I'm having trouble with):

import csv, math
with open("mydata.csv", "rb") as f:
vectors = csv.reader(f, delimiter=";")

    for col in vectors:
        x = float(col[0])
        y = float(col[1])
        dX = float(col[2])
        dY = float(col[3])
        magnitude = float(col[4])

        if dX > 0 and dY > 0:
            comp = dY/dX
            theta = math.degrees(math.atan(comp))
            angle = 90 - theta
        elif dX > 0 and dY < 0:
            comp = dY/dX
            theta = math.degrees(math.atan(comp))
            angle = 90 + theta
        elif dX < 0 and dY > 0:
            comp = dX/dY
            theta = math.degrees(math.atan(comp))
            angle = 360 - theta
        elif dX < 0 and dY < 0:
            comp = dY/dX
            theta = math.degrees(math.atan(comp))
            angle = 270 - theta

So essentially, I want to add the angle variable to a 6th column, for the correct line of my csv file.

I tried to create a new list and append (e.g.):

angles = []
...
angles.append(col)
angles.append(angle)

However, as you may have guessed I ended up with a line like this:

[[x, y, dX, dY, magnitude], angle]

Thanks for your help in advance.

MACooperr
  • 75
  • 1
  • 8

3 Answers3

1

col is a list itself, so you'd extend angles:

angles.extend(col)
angles.append(angle)

where list.extend() copies over the elements into the angles list, rather than add a single reference to the col list object.

If all you do is produce a new row with one value added, just re-use col and append to it directly:

col.append(angle)

and write that to your output CSV file.

col is misnamed, really, I'd call it row instead.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

This answer is too late as a solution has already been accepted, but the simplest way to solve the problem is to write the newly-constructed row directly to the output csv file without creating an intermediate list.

You could write something like:

import csv, math

with open("mydata.csv", "rb") as f,\
     open("newdata.csv", "wb") as g:
        vectors = csv.reader(f, delimiter=";")
        writer = csv.writer(g, delimiter=";")
        for row in vectors:
            # use destructuring
            x, y, dX, dY, magnitude = map(float, row)

            if dX > 0 and dY > 0:
            #<snip>

            # at this stage you can write directly to the output
            # file.
            writer.writerow([x, y, dX, dY, magnitude, angle])
superjump
  • 151
  • 4
  • Whilst I accepted Martijn's solution - I believe this more efficiently solves the problem. Many thanks. – MACooperr May 07 '14 at 14:08
  • 1
    @MACooperr: This solves the problem in much the same way, actually. Neither is more efficient than the other, in any case. Not that I am begrudging superjump the accept here. :-) – Martijn Pieters May 07 '14 at 14:09
  • @MartijnPieters -- I suppose this is true! I can only apologise! Thank you both for your help. With the initial solution you provided I had trouble writing to csv whilst maintaining the original columns (it presented as one long list); however, this is likely due to my own ineptitude, rather than your solution. – MACooperr May 07 '14 at 14:11
0

since col is list . u can just copy list items to angles and append it

angles=col[:]
angles.append(angle)
sundar nataraj
  • 8,524
  • 2
  • 34
  • 46