Me4President
Technical User
The problem:
I want to enter a text file, in the format below:
And I want to create a new file, with the format below:
i.e. a string, followed by 12 numbers on each line. The numbers are the first and third 4 digits of the input file - for example the first two numbers in the row are
from the first line
This is my attempt so far - I dont know how to loop over 6 lines, write this to one output line, and carry on until I reach the end of the input file. The input file will vary in length, and the output file must have the specified string, followed by 6 pairs of numbers on each line.
Any help would be greatly appreciated!
I want to enter a text file, in the format below:
Code:
3974.1399 1693.0822 1921.5103 1 3 3 event_outline
3877.0623 1658.3441 1854.6477 2 3 3 event_outline
3818.4111 1641.9969 1769.8671 2 3 3 event_outline
2461.6321 1632.2740 1836.2170 2 3 3 event_outline
2405.4290 1687.7454 1903.2582 2 3 3 event_outline
2347.6643 1721.6591 1968.0997 2 3 3 event_outline
2287.6213 1784.1433 1968.1174 2 3 3 event_outline
2287.6213 2512.1030 1634.8781 2 3 3 event_outline
And I want to create a new file, with the format below:
Code:
POLGON 3974 1693 3877 1658 3818 1641 2461 1632 2405 1687 2347 1721
Code:
3974 & 1693
Code:
3974.1399 1693.0822 1921.5103 1 3 3 event_outline
Code:
#!/usr/bin/python
filename = raw_input('input file? ')
file = open(filename,"r")
fileout = raw_input('file out? ')
#file = open(fileout,"wb")
#fo = file.readline() # trying to use readline()
sipstr = raw_input('SIPMAP string? ')
fa = open(fileout,"a") # to append each new number to file... Not ideal..
fa.write (sipstr) # write the string to each new line
fa.write (" ") # give the correct spaces after the string
lines = 0
for line in file: # looping over each line
x = line[6:10] # get first number
y = line[21:25] # get second number
fa.write (x)
fa.write (" ")
fa.write (y)
fa.write (" ")
if lines == 6: # when done 6 lines, make new line and start again
fa.write ("\n")
fa.write (sipstr)
fa.write (" ")
lines += 1
print 'Input file %r has %r pairs\n' % (filename, lines)
print 'Output file %r will have %r lines\n' % (filename, 1+lines/6)
fa.close()
file.close()
raw_input('Press enter to quit... ')
Any help would be greatly appreciated!