Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import argparse
- import os
- import subprocess
- # Use with:
- # $ ls | grep txt | grep -v sample | xargs -I {} ./generate.py {} {}
- script_path = os.path.dirname(os.path.realpath(__file__))
- shift_jis = "shift_jisx0213"
- parser = argparse.ArgumentParser()
- parser.add_argument("input")
- parser.add_argument("output")
- args = parser.parse_args()
- input_file = args.input
- output_file = args.output
- def read_shift_jis(file_path):
- with open(file_path, "r", encoding=shift_jis) as f:
- content = f.readlines()
- content = [x.strip() for x in content]
- return content
- print("Reading sample.txt and parsing headers...")
- sample_lines = read_shift_jis(script_path + "/sample.txt")
- headers = list(filter(lambda x: x.startswith("%txt"), sample_lines))
- input_lines = read_shift_jis(input_file)
- def get_actual_header(line):
- return line.split(" ")[0]
- print("Reading %s..." % input_file)
- curr_header = None
- header_lines = {}
- for line in input_lines:
- if len(line.strip()) == 0:
- continue
- if line.startswith("%txt"):
- curr_header = get_actual_header(line)
- header_lines[curr_header] = []
- else:
- header_lines[curr_header].append(line)
- print("Writing %s..." % output_file)
- with open(output_file, "w", encoding=shift_jis) as f:
- for header in headers:
- f.write(header + "\n")
- actual_header = get_actual_header(header)
- if actual_header in header_lines:
- for line in header_lines[actual_header]:
- f.write(line + "\n")
- f.write("\n")
- f.write("%endTxt\n")
- subprocess.run("unix2dos %s" % output_file, shell=True, check=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement