
June 29th, 2012, 02:09 PM
|
 |
Contributing User
|
|
|
|
|
2 solutions
Code:
# an approach using split.
# the with statement handles errors and closes the file.
with open('in', mode='r') as input_stream:
Data = input_stream.readlines()
# remove first line if it was really as you say
discard = "'INPUT FILE'"
if Data[0].startswith(discard):
del Data[0]
with open('out.split', mode='w') as output_stream:
output_stream.write("'OUTPUT FILE'\n")
for line in Data:
output_stream.write('\n'.join(line.split())+'\n')
###33333333333##########################
# More efficient solution. Replace the spaces with new lines.
with open('in', mode='r') as input_stream:
Data = input_stream.read()
# remove first line if it was really as you say
discard = "'INPUT FILE'\n"
i = 0
if Data.startswith(discard):
i = len(discard)
with open('out.replace', mode='w') as output_stream:
output_stream.write("'OUTPUT FILE'\n")
output_stream.write(Data[i:].replace(' ','\n'))
__________________
[code] Code tags[/code] are essential for python code!
|