| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 | 
|  | 2 |  | 
|  | 3 | import sys | 
|  | 4 |  | 
|  | 5 | def remove_comment(file_from, file_to, encoding): | 
|  | 6 | fd = open(file_from, "r", encoding=encoding) | 
|  | 7 | fd2 = open(file_to, "wb") | 
|  | 8 |  | 
|  | 9 | line_no=0 | 
|  | 10 | for line in fd.readlines(): | 
|  | 11 | #line = line.rstrip('\r\n').rstrip('\n') | 
|  | 12 | line_no += 1 | 
|  | 13 | if line_no == 1: | 
|  | 14 | fd2.write(line.encode()) | 
|  | 15 | continue | 
|  | 16 | line_s = line.strip() | 
|  | 17 | if len(line_s) == 0: | 
|  | 18 | fd2.write("\n".encode()) | 
|  | 19 | continue | 
|  | 20 | if line_s[0] == '#': | 
|  | 21 | #fd2.write("\n".encode()) | 
|  | 22 | continue | 
|  | 23 | else: | 
|  | 24 | fd2.write(line.encode()) | 
|  | 25 |  | 
|  | 26 | fd.close() | 
|  | 27 | fd2.close() | 
|  | 28 | return True | 
|  | 29 |  | 
|  | 30 | if __name__ == '__main__': | 
|  | 31 | file_from=sys.argv[1] | 
|  | 32 | file_to=sys.argv[2] | 
|  | 33 | try: | 
|  | 34 | remove_comment(file_from, file_to, 'gb18030') | 
|  | 35 | except: | 
|  | 36 | try: | 
|  | 37 | remove_comment(file_from, file_to, 'utf-8') | 
|  | 38 | except: | 
|  | 39 | print("[error]remove_comment" + file_from) | 
|  | 40 | sys.exit(-1) | 
|  | 41 |  |