Skip to content

Instantly share code, notes, and snippets.

@Mahdisadjadi
Last active August 29, 2015 14:21
Show Gist options
  • Save Mahdisadjadi/67fabb37de89190f0f52 to your computer and use it in GitHub Desktop.
Save Mahdisadjadi/67fabb37de89190f0f52 to your computer and use it in GitHub Desktop.
This code is written to import a file, start to read the lines after a particular line and stop on a particular line. Finally results are converted to float numbers and written in a file.
'''
File Refiner
May 18 2015
This code is written to import a file, start to read the lines after a particular line
and stop on a particular line. Finally results will convert to float number and write
in a file.
Assuming the code and file.crd are in the same directory, and we want to miss first 5 lines
and extract 239 lines after that, one should type:
> Python file.crd 5 239
results in file.crd_refined in the same directory.
-- Mahdi Sadjadi --
'''
import csv,sys
imported_file = sys.argv[1]
starting_line = eval(sys.argv[2]) # number of lines that one needs to skip
required_coordinates = eval(sys.argv[3]) # number of required lines from the file
ending_line = starting_line + required_coordinates + 1 # last line to be returned
f = open(imported_file,"r") # importing the original file
g = open(imported_file+"_refined","w") # exporting the refined file
linesCounter = 1
for line in f:
if linesCounter > starting_line and linesCounter < ending_line:
numbers = map(float,line.split()) # converting to float
writer = csv.writer(g, delimiter=' ') # setting the delimiter
writer.writerow(map("{0:0.6f}".format,numbers)) # setting precision of exported numbers
linesCounter += 1
f.close()
g.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment