Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting lines in XYZ file?

Status
Not open for further replies.

athomo

Technical User
Jan 31, 2005
2
US
First off, is it possible to write a script to delete certain lines within an XYZ file; I've only be able to find ways to delete rows within a table.

You see, I used Python (and a previously compiled script) to convert an ASCII file into an XYZ file (pretty cool!), but now i've got an 90mb file with z- values of -9999 that need to be deleted along with the corresponding x- and y- values. Note, I am unable to delete the -9999 values before conversion because then the ASCII to XYZ script would not run properly.

Any suggestions on how to run this -- sort first and then delete? make a table?

Here's an example file:
mydata.xyz
112.45,33.58,345.2
112.85,33.95,405.2
112.65,33.54,-9999 (...need this entire row deleted)
112.85,33.24,302.6

Thanks for any input you can give!
 
You would have to open the file, iterate through it, find the lines you want to keep and write them out to another file.
Code:
import csv

p = csv.reader( open( "mydata.xyz" ) )
o = open( "newdata.xyz", "w" )

for row in p:
  if row[2] != "-9999":
    o.write( ",".join( row ) . "\n" )
(I just wrote this off the top of my head, it isn't tested, but it should give you a start)
 
Thanks so much ~ the only change to this script was replacing a . with + as shown:

( row ) . "\n" )
with
( row ) + "\n" )

and it worked perfectly!

now it's time to play!! thanks again!
 
Oh, right. I've been switching back and forth from PHP too much. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top