100 lines
3.7 KiB
Python
100 lines
3.7 KiB
Python
import re, os, csv
|
|
import sys
|
|
import tomli
|
|
import unidecode
|
|
|
|
# 解析
|
|
def split_by_brackets(file_path):
|
|
with open(file_path, 'r') as file:
|
|
content = file.read()
|
|
result = content.replace('{','@{@').replace('}','@}@').replace(' ','@ @').replace('\n','@\n@').replace('\t','@\t@').replace('=','@=@')
|
|
result = result.split('@')
|
|
filtered_list = [string for string in result if string != ""]
|
|
dry_list = [string for string in result if string != "" and string != " " and string != "\t" and string != "\n"]
|
|
return filtered_list, dry_list
|
|
|
|
# 获取首个provinces
|
|
def get_index_provinces(file_path):
|
|
return split_by_brackets(file_path)[1][(split_by_brackets(file_path)[1].index('id') + 2)], split_by_brackets(file_path)[1][(split_by_brackets(file_path)[1].index('provinces') + 3)]
|
|
# 返回 states, provinces
|
|
def get_region_provinces(file_path):
|
|
res_lists = split_by_brackets(file_path)[1]
|
|
startindex = res_lists.index('provinces') + 3
|
|
provinces_list = []
|
|
while True:
|
|
provinces_list.append(res_lists[startindex])
|
|
startindex += 1
|
|
if res_lists[startindex] == '}':
|
|
break
|
|
return provinces_list
|
|
|
|
# 在数据查找
|
|
def search_by_provinces_get_region(file_path, region_path):
|
|
res = []
|
|
states, provinces = get_index_provinces(file_path)
|
|
for item in os.listdir(region_path):
|
|
full_region_path = os.path.join(region_path, item)
|
|
if os.path.isfile(full_region_path):
|
|
brakets_lists = split_by_brackets(full_region_path)[1]
|
|
region_provinces_path = get_region_provinces(full_region_path)
|
|
# print(provinces)
|
|
if provinces in region_provinces_path:
|
|
res.append({states: brakets_lists[brakets_lists.index('id') + 2]})
|
|
return res
|
|
|
|
# def search_by_provinces_get_region():
|
|
|
|
def exec_all_states_file(state_path, region_path):
|
|
res = []
|
|
for item in os.listdir(state_path):
|
|
full_state_path = os.path.join(state_path, item)
|
|
if os.path.isfile(full_state_path):
|
|
print(full_state_path)
|
|
for item in search_by_provinces_get_region(full_state_path, region_path):
|
|
res.append(item)
|
|
return res
|
|
|
|
# import data/sairen.csv
|
|
def csv_to_tuples(file_path):
|
|
tuples_list = []
|
|
with open(file_path, mode='r', newline='') as csvfile:
|
|
reader = csv.reader(csvfile)
|
|
|
|
for row in reader:
|
|
tuples_list.append({row[0]: row[1]})
|
|
#return tuples_list
|
|
return {k: v for d in tuples_list for k, v in d.items()}
|
|
|
|
def access_sir_list(original_dict, f):
|
|
try:
|
|
return original_dict[f]
|
|
except KeyError:
|
|
return 'N/A'
|
|
|
|
|
|
# file_path = sys.argv[1]
|
|
# region_path = sys.argv[2]
|
|
|
|
file_path = 'src/map/'
|
|
region_path = 'src/strategicregions/'
|
|
|
|
sir_list = csv_to_tuples('data/sairen.csv')
|
|
merged_dict = {k: access_sir_list(sir_list, v) for d in exec_all_states_file(file_path, region_path) for k, v in d.items()}
|
|
def dict_to_csv(data_dict, filename):
|
|
with open(filename, mode='w', newline='') as file:
|
|
writer = csv.writer(file)
|
|
for key, value in data_dict.items():
|
|
writer.writerow([key, value])
|
|
dict_to_csv(merged_dict, 'release/sairen_merged.csv')
|
|
|
|
def process_sir_txt_files(directory):
|
|
for root, dirs, files in os.walk(directory):
|
|
for file in files:
|
|
if file.endswith('.txt'):
|
|
sp_lists = split_by_brackets(os.path.join(root, file))[1]
|
|
tempindex = sp_lists.index('id') + 2
|
|
os.system('sed' + ' -i s/ZZZ/' + str(merged_dict[sp_lists[tempindex]]) + '/g ' +'\"' + str(os.path.join(root, file)) + '\"')
|
|
|
|
# 示例用法
|
|
sairen_path = 'release/sairen' # 替换为你的文件夹路径
|
|
process_sir_txt_files(sairen_path) |