-1

I have file containing random x,y,z coordinates. I am trying to write some code to insert a "-"(negative figure) in front of the z value (therefore giving me a negative elevation).

the problem is, I am saving the file as a .csv, unfortunately this does not add any delimiters, so the data is just on one cell per row separated by a space: x_y_z. The "csv" file contains over 1 million points, so editing it in excel is out of the question.

I have tried a couple of methods to do what I want, but i'm falling over when trying to iterate through every row and to write out the data. Any help or pointers would be greatly appreciated.

I've tried 2 things. the first method is probably extremely crude, but it seems to achieve what I want, although it only works on 1 line. I don't know how to really iterate through every line.

The second method will take the first line, and separate the X, Y and Z into different variables, after that I'm lost!

#First method:
import csv
inputfile = csv.reader(open ("\\wk.csv", 'r'))
    for row in inputfile:
    rowstring = str(row)
    x1 = rowstring[2:11]
    y1 = rowstring[12:22]
    z1 = "-" + rowstring[23:29]
    xynz = x1 + " " + y1 + " " + z1


#second method:
with open ("\\wk.csv", 'r+') as f:
    for l in f:
    x2, y2, z2 = l.split()
    z3 = "-"+z2

The first method will give me the result I need for one line only.['555555.55', '4444444.44', '-333.33']

The second method splits out the Z where I can add a negative, but after i'm stuck

MaartenDev
  • 5,631
  • 5
  • 21
  • 33
N P
  • 1
  • 1
  • Provide sample data from your `wk.csv` file and desired result. Also your indentation of posted code is incorrect so it is non-functional. Make a [mcve]. – Mark Tolonen Sep 11 '19 at 16:18

2 Answers2

0

If I understood well your problem, this script should work:

input_file = 'wk.csv'
output_file = 'new_wk.csv'

# Reading input
f = open(input_file, 'r')
rows = f.read().split('\n')
if '' in rows: # remove last blank line if needed
    rows.remove('')
f.close()

# Writing output
f = open(output_file, 'w')
for row in rows: # loop over your million rows
    print(row)
    x, y, z = row.split(' ')
    z = '-'+z    # editing z
    f.write('{} {} {}\n'.format(x, y, z))
f.close()
Adrien Pavao
  • 452
  • 5
  • 11
  • Thank you so much! I've altered the code, it starts to perform correctly but then falls after writing 25kb or so with the following: x, y, z = row.split(' ') ValueError: too many values to unpack (expected 3) – N P Sep 12 '19 at 07:45
  • Did you manage to solve this? I think thanks to `print(row)` you should be able to know why this is failing. Basically you have more than 2 spaces in your row so `row.split(' ')` has more than 3 values to 'unpack'. You can correct this by using the 3 first values with `values = row.split(' ')` and then `f.write('{} {} {}\n'.format(values[0], values[1], values[2]))`. – Adrien Pavao Oct 29 '19 at 20:46
0

This is how I am visualizing your data file

x1,y1,z1
x2,y2,z2
x3,y3,z3
    .
    .
    .

I was a little confused how you were describing your data file. Was it delimited by spaces? If so you can instead read in the data like so.

with open('input.csv') as csv_file:
     csv_reader = csv.reader(csv_file, delimiter=' ')

If it is in fact delimited by , then do.

with open('employee_birthday.txt') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')

What you can do is loop through and pull x y and z from each row like so.

import csv

with open('new.csv', mode='w') as out_file:
    csv_writer = csv.writer(out_file, delimiter=',')


#initialize new csv file.
for x, y, z in input_file:
    #I assume you want to read them as string to add the -
    x = str(x)
    y = str(y)
    z = str(z)
    # add the -
    z = "-" + z
    csv_writer.writerow([x, y, z])
kingkyle
  • 111
  • 1
  • 8