Python LMF library
 All Classes Namespaces Files Functions Variables
eol.py
Go to the documentation of this file.
1 #/usr/bin/python
2 
3 """! @package utils.eol
4 """
5 
6 # Get command line arguments
7 from optparse import OptionParser
8 parser = OptionParser()
9 parser.add_option("-i", "--input", dest="input", action="store", help="input MDF file")
10 parser.add_option("-o", "--output", dest="output", action="store", help="output MDF file")
11 options = parser.parse_args()[0]
12 
13 # Open input and output files
14 try:
15  in_file = open(options.input, "r", encoding='utf-8')
16  out_file = open(options.output, "w", encoding='utf-8')
17 except TypeError:
18  in_file = open(options.input, "r")
19  out_file = open(options.output, "w")
20 
21 # Define EOL depending on operating system
22 import os
23 if os.name == 'posix':
24  # Unix-style end of line
25  EOL = '\n'
26 else:
27  # Windows-style end of line
28  EOL = '\r\n'
29 
30 # Merge lines by deleting unwanted EOL
31 lines = []
32 for line in in_file.readlines():
33  # Do not parse empty lines
34  if line != EOL and not line.startswith('\\'):
35  previous_line = lines.pop()
36  line = previous_line.replace(EOL, " ") + line
37  lines.append(line)
38 for line in lines:
39  out_file.write(line)
40 
41 # Do not forget to close files
42 in_file.close()
43 out_file.close()