python-table-converter 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- python_table_converter-0.1.0.dist-info/LICENSE +21 -0
- python_table_converter-0.1.0.dist-info/METADATA +24 -0
- python_table_converter-0.1.0.dist-info/RECORD +8 -0
- python_table_converter-0.1.0.dist-info/WHEEL +4 -0
- python_table_converter-0.1.0.dist-info/entry_points.txt +4 -0
- table_converter/cli.py +50 -0
- table_converter/commands/convert_tables.py +68 -0
- table_converter/core/convert.py +345 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Akiva Miura
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: python-table-converter
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A table data converter
|
|
5
|
+
License: MIT
|
|
6
|
+
Author: Akiva Miura
|
|
7
|
+
Author-email: akiva.miura@gmail.com
|
|
8
|
+
Requires-Python: >=3.10,<4.0
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Requires-Dist: icecream (>=2.1.3,<3.0.0)
|
|
15
|
+
Requires-Dist: logzero (>=1.7.0,<2.0.0)
|
|
16
|
+
Requires-Dist: openpyxl (>=3.1.5,<4.0.0)
|
|
17
|
+
Requires-Dist: pandas (>=2.2.3,<3.0.0)
|
|
18
|
+
Requires-Dist: tqdm (>=4.67.0,<5.0.0)
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# Table Data Converter
|
|
22
|
+
|
|
23
|
+
This is a python-based tool that converts tables from one format to another. It can convert tables from CSV, TSV, Excel, JSON, and JSON Lines to any of these formats.
|
|
24
|
+
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
table_converter/cli.py,sha256=zTPqsFrpBuAaO9C5pENCBDKmkkHOiv5GMhMCdRqlH2w,1174
|
|
2
|
+
table_converter/commands/convert_tables.py,sha256=czwj0Yiy-cTk-d4ezQGLWXtm6wr0NrurT4lylWKh_G8,1637
|
|
3
|
+
table_converter/core/convert.py,sha256=oLyNbqmeex3GhQ6FuzOBH7uQV--1O9lWuZr9aPGPXuk,10714
|
|
4
|
+
python_table_converter-0.1.0.dist-info/LICENSE,sha256=1_Gn0I1neLPxDLfLiHEyxjDg-pbAra1p2iHEqutGS_c,1068
|
|
5
|
+
python_table_converter-0.1.0.dist-info/METADATA,sha256=cRBykOlAwuGHuGSp4tSQN92znaHAm5sCBeZ-z4H888U,880
|
|
6
|
+
python_table_converter-0.1.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
|
7
|
+
python_table_converter-0.1.0.dist-info/entry_points.txt,sha256=VL9tNIWL31AGIvJnHetcJGH7G1i_WNcOknDpYq6JRac,117
|
|
8
|
+
python_table_converter-0.1.0.dist-info/RECORD,,
|
table_converter/cli.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
import argparse
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
from icecream import ic
|
|
8
|
+
|
|
9
|
+
def parse_and_run(
|
|
10
|
+
parser: argparse.ArgumentParser,
|
|
11
|
+
):
|
|
12
|
+
ic.disable()
|
|
13
|
+
ic()
|
|
14
|
+
args = parser.parse_args()
|
|
15
|
+
ic(args)
|
|
16
|
+
if args.handler:
|
|
17
|
+
args.handler(args)
|
|
18
|
+
else:
|
|
19
|
+
parser.print_help()
|
|
20
|
+
sys.exit(1)
|
|
21
|
+
|
|
22
|
+
def command_convert_tables(
|
|
23
|
+
parser: argparse.ArgumentParser|None = None,
|
|
24
|
+
):
|
|
25
|
+
if parser is None:
|
|
26
|
+
command_parser = argparse.ArgumentParser(
|
|
27
|
+
description='Convert a table to a different format.'
|
|
28
|
+
)
|
|
29
|
+
else:
|
|
30
|
+
command_parser = parser
|
|
31
|
+
from . commands.convert_tables import setup_parser
|
|
32
|
+
setup_parser(command_parser)
|
|
33
|
+
if parser is None:
|
|
34
|
+
parse_and_run(parser)
|
|
35
|
+
|
|
36
|
+
def main():
|
|
37
|
+
parser = argparse.ArgumentParser(description='Table Data Converter')
|
|
38
|
+
parser.set_defaults(handler=None)
|
|
39
|
+
subparsers = parser.add_subparsers(dest='command')
|
|
40
|
+
|
|
41
|
+
parser_convert_tables = subparsers.add_parser(
|
|
42
|
+
'convert',
|
|
43
|
+
help='Convert a table to a different format.'
|
|
44
|
+
)
|
|
45
|
+
command_convert_tables(parser_convert_tables)
|
|
46
|
+
|
|
47
|
+
parse_and_run(parser)
|
|
48
|
+
|
|
49
|
+
if __name__ == '__main__':
|
|
50
|
+
main()
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
|
|
5
|
+
from icecream import ic
|
|
6
|
+
|
|
7
|
+
from .. core.convert import convert
|
|
8
|
+
|
|
9
|
+
def run(
|
|
10
|
+
args: argparse.Namespace,
|
|
11
|
+
):
|
|
12
|
+
convert(
|
|
13
|
+
input_files = args.input_files,
|
|
14
|
+
output_file = args.output_file,
|
|
15
|
+
assign_constants = args.assign_constants,
|
|
16
|
+
assign_formats = args.assign_formats,
|
|
17
|
+
pickup_columns= args.pickup_columns,
|
|
18
|
+
fields_to_split_by_newline = args.split_by_newline,
|
|
19
|
+
fields_to_assign_ids = args.assign_ids,
|
|
20
|
+
output_debug = args.output_debug,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
def setup_parser(
|
|
24
|
+
parser: argparse.ArgumentParser,
|
|
25
|
+
):
|
|
26
|
+
parser.add_argument(
|
|
27
|
+
'input_files',
|
|
28
|
+
metavar='INPUT_FILE',
|
|
29
|
+
nargs='+',
|
|
30
|
+
help='Path to the input file.'
|
|
31
|
+
)
|
|
32
|
+
parser.add_argument(
|
|
33
|
+
'-o', '--output-file',
|
|
34
|
+
metavar='OUTPUT_FILE',
|
|
35
|
+
required=True,
|
|
36
|
+
help='Path to the output file.'
|
|
37
|
+
)
|
|
38
|
+
parser.add_argument(
|
|
39
|
+
'--pickup-columns',
|
|
40
|
+
type=str,
|
|
41
|
+
help='Pickup column map',
|
|
42
|
+
)
|
|
43
|
+
parser.add_argument(
|
|
44
|
+
'--split-by-newline',
|
|
45
|
+
type=str,
|
|
46
|
+
help='Fields to split by newline',
|
|
47
|
+
)
|
|
48
|
+
parser.add_argument(
|
|
49
|
+
'--assign-ids',
|
|
50
|
+
type=str,
|
|
51
|
+
help='Field to assign ids',
|
|
52
|
+
)
|
|
53
|
+
parser.add_argument(
|
|
54
|
+
'--assign-constants',
|
|
55
|
+
type=str,
|
|
56
|
+
help='Field to assign constants',
|
|
57
|
+
)
|
|
58
|
+
parser.add_argument(
|
|
59
|
+
'--assign-formats',
|
|
60
|
+
type=str,
|
|
61
|
+
help='Field to assign formats',
|
|
62
|
+
)
|
|
63
|
+
parser.add_argument(
|
|
64
|
+
'--output-debug',
|
|
65
|
+
action='store_true',
|
|
66
|
+
help='Output debug information',
|
|
67
|
+
)
|
|
68
|
+
parser.set_defaults(handler=run)
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
|
|
6
|
+
from collections import OrderedDict
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
import pandas as pd
|
|
10
|
+
from icecream import ic
|
|
11
|
+
|
|
12
|
+
dict_loaders: dict[str, callable] = {}
|
|
13
|
+
def register_loader(
|
|
14
|
+
ext: str,
|
|
15
|
+
):
|
|
16
|
+
def decorator(loader):
|
|
17
|
+
dict_loaders[ext] = loader
|
|
18
|
+
return loader
|
|
19
|
+
return decorator
|
|
20
|
+
|
|
21
|
+
dict_savers: dict[str, callable] = {}
|
|
22
|
+
def register_saver(
|
|
23
|
+
ext: str,
|
|
24
|
+
):
|
|
25
|
+
def decorator(saver):
|
|
26
|
+
dict_savers[ext] = saver
|
|
27
|
+
return saver
|
|
28
|
+
return decorator
|
|
29
|
+
|
|
30
|
+
@register_loader('.xlsx')
|
|
31
|
+
def load_excel(
|
|
32
|
+
input_file: str,
|
|
33
|
+
):
|
|
34
|
+
df = pd.read_excel(input_file)
|
|
35
|
+
return df
|
|
36
|
+
|
|
37
|
+
@register_saver('.json')
|
|
38
|
+
def save_json(
|
|
39
|
+
df: pd.DataFrame,
|
|
40
|
+
output_file: str,
|
|
41
|
+
):
|
|
42
|
+
# NOTE: この方法だとスラッシュがすべてエスケープされてしまった
|
|
43
|
+
#df.to_json(
|
|
44
|
+
# output_file,
|
|
45
|
+
# orient='records',
|
|
46
|
+
# force_ascii=False,
|
|
47
|
+
# indent=2,
|
|
48
|
+
# escape_forward_slashes=False,
|
|
49
|
+
#)
|
|
50
|
+
#ic(df.iloc[0])
|
|
51
|
+
data = df.to_dict(orient='records')
|
|
52
|
+
with open(output_file, 'w') as f:
|
|
53
|
+
json.dump(
|
|
54
|
+
data,
|
|
55
|
+
f,
|
|
56
|
+
indent=2,
|
|
57
|
+
ensure_ascii=False,
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
@register_saver('.jsonl')
|
|
61
|
+
def save_jsonl(
|
|
62
|
+
df: pd.DataFrame,
|
|
63
|
+
output_file: str,
|
|
64
|
+
):
|
|
65
|
+
# NOTE: この方法だとスラッシュがすべてエスケープされてしまった
|
|
66
|
+
#df.to_json(
|
|
67
|
+
# output_file,
|
|
68
|
+
# orient='records',
|
|
69
|
+
# lines=True,
|
|
70
|
+
# force_ascii=False,
|
|
71
|
+
#)
|
|
72
|
+
with open(output_file, 'w') as f:
|
|
73
|
+
for index, row in df.iterrows():
|
|
74
|
+
data = row.to_dict()
|
|
75
|
+
json.dump(
|
|
76
|
+
data,
|
|
77
|
+
f,
|
|
78
|
+
ensure_ascii=False,
|
|
79
|
+
)
|
|
80
|
+
f.write('\n')
|
|
81
|
+
|
|
82
|
+
def set_field_value(
|
|
83
|
+
data: OrderedDict,
|
|
84
|
+
field: str,
|
|
85
|
+
value: any,
|
|
86
|
+
):
|
|
87
|
+
if '.' in field:
|
|
88
|
+
field, rest = field.split('.', 1)
|
|
89
|
+
if field not in data:
|
|
90
|
+
data[field] = OrderedDict()
|
|
91
|
+
set_field_value(data[field], rest, value)
|
|
92
|
+
else:
|
|
93
|
+
data[field] = value
|
|
94
|
+
|
|
95
|
+
def get_field_value(
|
|
96
|
+
data: OrderedDict,
|
|
97
|
+
field: str,
|
|
98
|
+
):
|
|
99
|
+
if field in data:
|
|
100
|
+
return data[field], True
|
|
101
|
+
if '.' in field:
|
|
102
|
+
field, rest = field.split('.', 1)
|
|
103
|
+
if field in data:
|
|
104
|
+
return get_field_value(data[field], rest)
|
|
105
|
+
return None, False
|
|
106
|
+
|
|
107
|
+
def search_column_value(
|
|
108
|
+
row: OrderedDict,
|
|
109
|
+
column: str,
|
|
110
|
+
):
|
|
111
|
+
if '__debug__' in row:
|
|
112
|
+
value, found = get_field_value(row['__debug__'], column)
|
|
113
|
+
if found:
|
|
114
|
+
return value, True
|
|
115
|
+
value, found = get_field_value(row['__debug__'], column)
|
|
116
|
+
original, found = get_field_value(row, '__debug__.__original__')
|
|
117
|
+
if found:
|
|
118
|
+
value, found = get_field_value(original, column)
|
|
119
|
+
if found:
|
|
120
|
+
return value, True
|
|
121
|
+
value, found = get_field_value(row, column)
|
|
122
|
+
if found:
|
|
123
|
+
set_field_value(row, column, value)
|
|
124
|
+
return value, True
|
|
125
|
+
return None, False
|
|
126
|
+
|
|
127
|
+
def map_constants(
|
|
128
|
+
row: OrderedDict,
|
|
129
|
+
dict_constants: OrderedDict,
|
|
130
|
+
):
|
|
131
|
+
new_row = OrderedDict(row)
|
|
132
|
+
for column in dict_constants.keys():
|
|
133
|
+
#set_field_value(new_row, column, dict_constants[column])
|
|
134
|
+
set_field_value(new_row, f'__debug__.{column}', dict_constants[column])
|
|
135
|
+
return new_row
|
|
136
|
+
|
|
137
|
+
def map_formats(
|
|
138
|
+
row: OrderedDict,
|
|
139
|
+
dict_formats: OrderedDict,
|
|
140
|
+
):
|
|
141
|
+
new_row = OrderedDict(row)
|
|
142
|
+
for column in dict_formats.keys():
|
|
143
|
+
template = dict_formats[column]
|
|
144
|
+
try:
|
|
145
|
+
params = {}
|
|
146
|
+
params.update(row['__debug__'])
|
|
147
|
+
params.update(row)
|
|
148
|
+
formatted = template.format(**params)
|
|
149
|
+
except KeyError as e:
|
|
150
|
+
ic(e)
|
|
151
|
+
formatted = template
|
|
152
|
+
raise e
|
|
153
|
+
set_field_value(new_row, f'__debug__.{column}', formatted)
|
|
154
|
+
return new_row
|
|
155
|
+
|
|
156
|
+
def remap_columns(
|
|
157
|
+
row: OrderedDict,
|
|
158
|
+
dict_remap: OrderedDict,
|
|
159
|
+
):
|
|
160
|
+
new_row = OrderedDict()
|
|
161
|
+
for column in dict_remap.keys():
|
|
162
|
+
value, found = search_column_value(row, dict_remap[column])
|
|
163
|
+
if found:
|
|
164
|
+
set_field_value(new_row, column, value)
|
|
165
|
+
for column in row.keys():
|
|
166
|
+
if column == '__debug__':
|
|
167
|
+
# NOTE: Ignore debug fields
|
|
168
|
+
set_field_value(new_row, column, row[column])
|
|
169
|
+
return new_row
|
|
170
|
+
|
|
171
|
+
def apply_fields_split_by_newline(
|
|
172
|
+
row: OrderedDict,
|
|
173
|
+
dict_fields: OrderedDict,
|
|
174
|
+
):
|
|
175
|
+
new_row = OrderedDict(row)
|
|
176
|
+
for column in dict_fields:
|
|
177
|
+
value, found = search_column_value(row, dict_fields[column])
|
|
178
|
+
#ic(value, found)
|
|
179
|
+
if found:
|
|
180
|
+
if isinstance(value, str):
|
|
181
|
+
new_value = value.split('\n')
|
|
182
|
+
set_field_value(new_row, f'__debug__.{column}', new_value)
|
|
183
|
+
else:
|
|
184
|
+
set_field_value(new_row, f'__debug__.{column}', value)
|
|
185
|
+
return new_row
|
|
186
|
+
|
|
187
|
+
def create_id_stat_node():
|
|
188
|
+
return {
|
|
189
|
+
'max_id': 0,
|
|
190
|
+
'dict_value_to_id': {},
|
|
191
|
+
'dict_id_to_node': {},
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
def assign_id_in_node(
|
|
195
|
+
row: OrderedDict,
|
|
196
|
+
column: str,
|
|
197
|
+
dict_assignment: OrderedDict,
|
|
198
|
+
id_stat_node: dict,
|
|
199
|
+
):
|
|
200
|
+
value, found = search_column_value(row, dict_assignment[column])
|
|
201
|
+
if not found:
|
|
202
|
+
raise KeyError(f'Column not found: {column}, existing columns: {row.keys()}')
|
|
203
|
+
if value not in id_stat_node['dict_value_to_id']:
|
|
204
|
+
field_id = id_stat_node['max_id'] + 1
|
|
205
|
+
id_stat_node['max_id'] = field_id
|
|
206
|
+
id_stat_node['dict_value_to_id'][value] = field_id
|
|
207
|
+
node = create_id_stat_node()
|
|
208
|
+
id_stat_node['dict_id_to_node'][field_id] = node
|
|
209
|
+
else:
|
|
210
|
+
field_id = id_stat_node['dict_value_to_id'][value]
|
|
211
|
+
node = id_stat_node['dict_id_to_node'][field_id]
|
|
212
|
+
set_field_value(row, f'__debug__.{column}', field_id)
|
|
213
|
+
set_field_value(row, f'__debug__.__ids__.{column}', field_id)
|
|
214
|
+
return node
|
|
215
|
+
|
|
216
|
+
def assign_id(
|
|
217
|
+
row: OrderedDict,
|
|
218
|
+
dict_assignment: OrderedDict,
|
|
219
|
+
root_id_stat_node: dict,
|
|
220
|
+
):
|
|
221
|
+
new_row = OrderedDict(row)
|
|
222
|
+
node = root_id_stat_node
|
|
223
|
+
for column in dict_assignment:
|
|
224
|
+
node = assign_id_in_node(new_row, column, dict_assignment, node)
|
|
225
|
+
return new_row
|
|
226
|
+
|
|
227
|
+
def convert(
|
|
228
|
+
input_files: list[str],
|
|
229
|
+
output_file: str | None = None,
|
|
230
|
+
assign_constants: str | None = None,
|
|
231
|
+
assign_formats: str | None = None,
|
|
232
|
+
pickup_columns: str | None = None,
|
|
233
|
+
fields_to_split_by_newline: str | None = None,
|
|
234
|
+
fields_to_assign_ids: str | None = None,
|
|
235
|
+
output_debug: bool = False,
|
|
236
|
+
):
|
|
237
|
+
ic()
|
|
238
|
+
ic(input_files)
|
|
239
|
+
df_list = []
|
|
240
|
+
dict_constants: OrderedDict | None = None
|
|
241
|
+
dict_columns: OrderedDict | None = None
|
|
242
|
+
dict_formats: OrderedDict | None = None
|
|
243
|
+
#list_fields_to_split_by_newline = None
|
|
244
|
+
dict_split_by_newline: OrderedDict | None = None
|
|
245
|
+
dict_assign_ids= None
|
|
246
|
+
root_id_stat = create_id_stat_node()
|
|
247
|
+
if assign_constants:
|
|
248
|
+
dict_constants = OrderedDict()
|
|
249
|
+
fields = assign_constants.split(',')
|
|
250
|
+
for field in fields:
|
|
251
|
+
if '=' in field:
|
|
252
|
+
dst, src = field.split('=')
|
|
253
|
+
dict_constants[dst] = src
|
|
254
|
+
else:
|
|
255
|
+
raise ValueError(f'Invalid constant assignment: {field}')
|
|
256
|
+
if assign_formats:
|
|
257
|
+
dict_formats = OrderedDict()
|
|
258
|
+
fields = assign_formats.split(',')
|
|
259
|
+
for field in fields:
|
|
260
|
+
if '=' in field:
|
|
261
|
+
dst, src = field.split('=')
|
|
262
|
+
dict_formats[dst] = src
|
|
263
|
+
else:
|
|
264
|
+
raise ValueError(f'Invalid template assignment: {field}')
|
|
265
|
+
if pickup_columns:
|
|
266
|
+
dict_columns = OrderedDict()
|
|
267
|
+
fields = pickup_columns.split(',')
|
|
268
|
+
for field in fields:
|
|
269
|
+
if '=' in field:
|
|
270
|
+
dst, value = field.split('=')
|
|
271
|
+
dict_columns[dst] = value
|
|
272
|
+
else:
|
|
273
|
+
dict_columns[field] = field
|
|
274
|
+
if fields_to_split_by_newline:
|
|
275
|
+
#list_fields_to_split_by_newline = fields_to_split_by_newline.split(',')
|
|
276
|
+
dict_split_by_newline = OrderedDict()
|
|
277
|
+
fields = fields_to_split_by_newline.split(',')
|
|
278
|
+
for field in fields:
|
|
279
|
+
if '=' in field:
|
|
280
|
+
dst, src = field.split('=')
|
|
281
|
+
dict_split_by_newline[dst] = src
|
|
282
|
+
else:
|
|
283
|
+
raise ValueError(f'Invalid split by newline: {field}')
|
|
284
|
+
if fields_to_assign_ids:
|
|
285
|
+
dict_assign_ids = OrderedDict()
|
|
286
|
+
fields = fields_to_assign_ids.split(',')
|
|
287
|
+
for field in fields:
|
|
288
|
+
if '=' in field:
|
|
289
|
+
dst, src = field.split('=')
|
|
290
|
+
dict_assign_ids[dst] = src
|
|
291
|
+
else:
|
|
292
|
+
raise ValueError(f'Invalid id assignment: {field}')
|
|
293
|
+
if output_file:
|
|
294
|
+
ext = os.path.splitext(output_file)[1]
|
|
295
|
+
if ext not in dict_savers:
|
|
296
|
+
raise ValueError(f'Unsupported file type: {ext}')
|
|
297
|
+
saver = dict_savers[ext]
|
|
298
|
+
for input_file in input_files:
|
|
299
|
+
ic(input_file)
|
|
300
|
+
if not os.path.exists(input_file):
|
|
301
|
+
raise FileNotFoundError(f'File not found: {input_file}')
|
|
302
|
+
ext = os.path.splitext(input_file)[1]
|
|
303
|
+
ic(ext)
|
|
304
|
+
if ext not in dict_loaders:
|
|
305
|
+
raise ValueError(f'Unsupported file type: {ext}')
|
|
306
|
+
df = dict_loaders[ext](input_file)
|
|
307
|
+
# NOTE: NaN を None に変換しておかないと厄介
|
|
308
|
+
df = df.replace([np.nan], [None])
|
|
309
|
+
#ic(df)
|
|
310
|
+
ic(len(df))
|
|
311
|
+
ic(df.columns)
|
|
312
|
+
ic(df.iloc[0])
|
|
313
|
+
new_rows = []
|
|
314
|
+
for index, row in df.iterrows():
|
|
315
|
+
orig = OrderedDict(row)
|
|
316
|
+
new_row = OrderedDict(row)
|
|
317
|
+
set_field_value(new_row, '__debug__.__original__', orig)
|
|
318
|
+
set_field_value(new_row, '__debug__.__file__', input_file)
|
|
319
|
+
if dict_constants:
|
|
320
|
+
new_row = map_constants(new_row, dict_constants)
|
|
321
|
+
if dict_columns:
|
|
322
|
+
new_row = remap_columns(new_row, dict_columns)
|
|
323
|
+
#if list_fields_to_split_by_newline:
|
|
324
|
+
# new_row = apply_fields_split_by_newline(new_row, list_fields_to_split_by_newline)
|
|
325
|
+
if dict_split_by_newline:
|
|
326
|
+
new_row = apply_fields_split_by_newline(new_row, dict_split_by_newline)
|
|
327
|
+
if dict_assign_ids:
|
|
328
|
+
new_row = assign_id(new_row, dict_assign_ids, root_id_stat)
|
|
329
|
+
if dict_formats:
|
|
330
|
+
new_row = map_formats(new_row, dict_formats)
|
|
331
|
+
if dict_columns:
|
|
332
|
+
new_row = remap_columns(new_row, dict_columns)
|
|
333
|
+
if not output_debug:
|
|
334
|
+
new_row.pop('__debug__', None)
|
|
335
|
+
new_rows.append(new_row)
|
|
336
|
+
new_df = pd.DataFrame(new_rows)
|
|
337
|
+
df_list.append(new_df)
|
|
338
|
+
all_df = pd.concat(df_list)
|
|
339
|
+
#ic(all_df)
|
|
340
|
+
ic(len(all_df))
|
|
341
|
+
ic(all_df.columns)
|
|
342
|
+
ic(all_df.iloc[0])
|
|
343
|
+
if output_file:
|
|
344
|
+
ic('Saing to: ', output_file)
|
|
345
|
+
saver(all_df, output_file)
|