extract-list 0.2__tar.gz
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.
- extract_list-0.2/LICENSE.txt +22 -0
- extract_list-0.2/PKG-INFO +180 -0
- extract_list-0.2/README.md +77 -0
- extract_list-0.2/README_pypi.md +159 -0
- extract_list-0.2/extract_list.egg-info/PKG-INFO +180 -0
- extract_list-0.2/extract_list.egg-info/SOURCES.txt +29 -0
- extract_list-0.2/extract_list.egg-info/dependency_links.txt +1 -0
- extract_list-0.2/extract_list.egg-info/requires.txt +7 -0
- extract_list-0.2/extract_list.egg-info/top_level.txt +1 -0
- extract_list-0.2/pyproject.toml +22 -0
- extract_list-0.2/setup.cfg +4 -0
- extract_list-0.2/setup.py +25 -0
- extract_list-0.2/src/extract_list/__init__.py +1 -0
- extract_list-0.2/src/extract_list/__main__.py +11 -0
- extract_list-0.2/src/extract_list/commontypes.py +24 -0
- extract_list-0.2/src/extract_list/config_enums.py +32 -0
- extract_list-0.2/src/extract_list/extract_cmd.py +144 -0
- extract_list-0.2/src/extract_list/extract_config.py +415 -0
- extract_list-0.2/src/extract_list/extract_data.py +306 -0
- extract_list-0.2/src/extract_list/extract_func.py +26 -0
- extract_list-0.2/src/extract_list/generate_cfg.py +160 -0
- extract_list-0.2/src/extract_list/generate_txt_ex_common.py +93 -0
- extract_list-0.2/src/extract_list/generate_txt_example_json.py +111 -0
- extract_list-0.2/src/extract_list/generate_txt_example_xml.py +131 -0
- extract_list-0.2/src/extract_list/generate_txt_sw_to_rrs.py +63 -0
- extract_list-0.2/src/extract_list/generate_txt_syntax.py +193 -0
- extract_list-0.2/src/extract_list/handle_input.py +80 -0
- extract_list-0.2/src/extract_list/handle_json_xml_output.py +46 -0
- extract_list-0.2/src/extract_list/handle_output.py +56 -0
- extract_list-0.2/src/extract_list/handle_txt_output.py +40 -0
- extract_list-0.2/src/extract_list/py.typed +2 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Tom Björkholm
|
|
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.
|
|
22
|
+
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: extract-list
|
|
3
|
+
Version: 0.2
|
|
4
|
+
Summary: Extract a list from JSON or XML, save to excel, csv, etc.
|
|
5
|
+
Author: Tom Björkholm
|
|
6
|
+
Author-email: Tom Björkholm <klausuler_linnet0q@icloud.com>
|
|
7
|
+
Project-URL: Source code, https://bitbucket.org/tom-bjorkholm/extract-list
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.12.6
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE.txt
|
|
14
|
+
Requires-Dist: excel-list-transform>=0.7.1
|
|
15
|
+
Requires-Dist: xmltodict>=0.13.0
|
|
16
|
+
Requires-Dist: types-xmltodict>=0.13.0.3
|
|
17
|
+
Requires-Dist: pip>=24.2
|
|
18
|
+
Requires-Dist: setuptools>=75.6.0
|
|
19
|
+
Requires-Dist: build>=1.2.2
|
|
20
|
+
Requires-Dist: wheel>=0.45.1
|
|
21
|
+
|
|
22
|
+
# extract-list
|
|
23
|
+
|
|
24
|
+
## Background
|
|
25
|
+
|
|
26
|
+
This python application was born out of the experience that needed data was available as part of JSON or as part of XML files, but the data was needed as a list of columns in excel or CSV (comma separated values) format.
|
|
27
|
+
|
|
28
|
+
## What it does
|
|
29
|
+
|
|
30
|
+
This small python application:
|
|
31
|
+
|
|
32
|
+
* reads data from an XML file or from a JSON file.
|
|
33
|
+
* extracts (a configurable part of the) data from the data read
|
|
34
|
+
* outputs the extracted data as list with a number of columns in the desired format that can be:
|
|
35
|
+
* Excel
|
|
36
|
+
* CSV (comma separated values)
|
|
37
|
+
* plain text file
|
|
38
|
+
* JSON
|
|
39
|
+
* XML
|
|
40
|
+
|
|
41
|
+
How this is done is governed by a configuration file. The application can create a number of example configuration files with accompanying description text files.
|
|
42
|
+
|
|
43
|
+
## Installing it
|
|
44
|
+
|
|
45
|
+
If you want to use it, install it using pip. A precondition is that you have Python 3.12.6 or newer installed on you computer. Python can be downloaded from [https://www.python.org/downloads/](https://www.python.org/downloads/).
|
|
46
|
+
|
|
47
|
+
### Installing on mac and Linux
|
|
48
|
+
|
|
49
|
+
````sh
|
|
50
|
+
pip3 install extract-list
|
|
51
|
+
````
|
|
52
|
+
|
|
53
|
+
### Installing on Microsoft Windows
|
|
54
|
+
|
|
55
|
+
````sh
|
|
56
|
+
pip install extract-list
|
|
57
|
+
````
|
|
58
|
+
|
|
59
|
+
## Running the application
|
|
60
|
+
|
|
61
|
+
### Running the application on mac and Linux
|
|
62
|
+
|
|
63
|
+
````sh
|
|
64
|
+
python3 -m extract_list --help
|
|
65
|
+
python3 -m extract_list cfg-example --help
|
|
66
|
+
python3 -m extract_list extract --help
|
|
67
|
+
python3 -m extract_list cfg-example -k sw_json_to_rrs -t excel -o sw-example.cfg
|
|
68
|
+
python3 -m extract_list extract -c sw-example.cfg -i input.json -o output.xlsx
|
|
69
|
+
````
|
|
70
|
+
|
|
71
|
+
### Running the application on Microsoft Windows
|
|
72
|
+
|
|
73
|
+
````sh
|
|
74
|
+
python -m extract_list --help
|
|
75
|
+
python -m extract_list cfg-example --help
|
|
76
|
+
python -m extract_list extract --help
|
|
77
|
+
python -m extract_list cfg-example -k sw_json_to_rrs -t excel -o sw-example.cfg
|
|
78
|
+
python -m extract_list extract -c sw-example.cfg -i input.json -o output.xlsx
|
|
79
|
+
````
|
|
80
|
+
|
|
81
|
+
## Suggested way to get started
|
|
82
|
+
|
|
83
|
+
1. Use the "cfg-example" sub-command to generate a few example configuration (.cfg) files with description (.txt) files.
|
|
84
|
+
2. Read the example configuration (.cfg) files and the accompanying description (.txt) files.
|
|
85
|
+
3. Find an example that is close to what you want to achieve.
|
|
86
|
+
4. Modify that configuration file to achieve what you want to achieve.
|
|
87
|
+
5. Use the "extract" sub-command to read your data and output extracted data according to your modified configuration file.
|
|
88
|
+
6. Read the produced output. If necessary go back to step 4 and adjust how the data is transformed.
|
|
89
|
+
|
|
90
|
+
### Example configuration files
|
|
91
|
+
|
|
92
|
+
When using the "cfg-example" sub-command to generate an example configuration file (say example.cfg) a text file describing the configuration and the syntax of the configuration file is also generated. If the example configuration file is named example.cfg, then the text file descriging the configuration is named example.txt.
|
|
93
|
+
|
|
94
|
+
You can generate several example configuration files each with an accompanying text file descriping it.
|
|
95
|
+
|
|
96
|
+
Read the text file describing the configuration file while looking at the configuration file to understand the syntax and the possible options.
|
|
97
|
+
|
|
98
|
+
## Description of how to write/change the configuration file
|
|
99
|
+
|
|
100
|
+
The configuration file is in JSON syntax [https://en.wikipedia.org/wiki/JSON](https://en.wikipedia.org/wiki/JSON).
|
|
101
|
+
The keywords and the nesting is important. The order of keywords have no significance (the examples use alphabetical order). Indentation and line breaks have no significance.
|
|
102
|
+
|
|
103
|
+
The encoding for the configuration file must be UTF-8. (US-ACII is a subset of UTF-8.)
|
|
104
|
+
|
|
105
|
+
It is recommended that you let the command generate a configuration file and then edit that file to match your needs. It is NOT recommended that the user writes the configuration file from scratch.
|
|
106
|
+
|
|
107
|
+
### Type of input file
|
|
108
|
+
|
|
109
|
+
The type of input file to read is determined by **"infile_type"**. **"infile_type"** can have the values:
|
|
110
|
+
|
|
111
|
+
* *"JSON"*
|
|
112
|
+
* *"XML"*
|
|
113
|
+
|
|
114
|
+
As both JSON and XML are syntax in text files, the text files can have an encoding for the text in the files
|
|
115
|
+
[https://en.wikipedia.org/wiki/Character_encoding](https://en.wikipedia.org/wiki/Character_encoding).
|
|
116
|
+
This is specified with **"infile_encoding"**. Unless you know that you need another encoding leave this as in the generated example configuration.
|
|
117
|
+
|
|
118
|
+
### Type of output file
|
|
119
|
+
|
|
120
|
+
The type of output file to produce is determined by **"outfile_type"**. **"outfile_type"** can have the values:
|
|
121
|
+
|
|
122
|
+
* *"EXCEL"*
|
|
123
|
+
* *"CSV"*
|
|
124
|
+
* *"JSON"*
|
|
125
|
+
* *"XML"*
|
|
126
|
+
* *"TXT"*
|
|
127
|
+
|
|
128
|
+
As *CSV, TXT, JSON* and *XML* are syntaxes in text files, the text files can have an encoding for the text in the files
|
|
129
|
+
[https://en.wikipedia.org/wiki/Character_encoding](https://en.wikipedia.org/wiki/Character_encoding).
|
|
130
|
+
This is specified with **"outfile_encoding"**. Unless you know that you need another encoding leave this as in the generated example configuration.
|
|
131
|
+
|
|
132
|
+
Comma separated values files (CSV files) may differ slightly depending on the programs used to read/write them and the locale used. **"out_csv_dialect"** changes how CSV files are written. It is always needed in the configuration file, but is only used if the output is CSV.
|
|
133
|
+
|
|
134
|
+
Excel files can be written using three libraries. **"outfile_excel_library"** can have values *"OPENPYXL"*, *"XLSXWRITER"* or *"PYLIGHTXL"*. These are different third party libraries that can read/write excel. My experience is that "PYLIGHTXL" most often is able to read and write excel files correctly. If you have trouble writing your particular excel file, please try another library. **"outfile_excel_library"** is always needed in the configuration file but is only used if the output is excel.
|
|
135
|
+
|
|
136
|
+
### Data to extract
|
|
137
|
+
|
|
138
|
+
The input files (JSON or XML) is likely to include a lot more data than what is interesting to extract. The data to extract is specified using **"main_line"** and **"linked_lines"** parameters in the configuration file.
|
|
139
|
+
|
|
140
|
+
The **"main_line"** specifies what part of the input file should be the main part of the output line(s). The linked lines have some data linking them to the main line: A linked line is linked to the main line if some item in the linked line has the same value as some other item in the main line.
|
|
141
|
+
|
|
142
|
+
The **"line"** sub-parameter of the the **"main_line"** and **"linked_lines"** configuration parameters includes a list of strings. This list of strings is the path of keywords to the records. Directly below this path is either a list or a dictionary of the records.
|
|
143
|
+
|
|
144
|
+
The dictionary of the records of the **"main_line"** are indexed in the input (either by an integer index in the case of a list, or by the key in case of a dictionary). If you want this index (also known as key) to be included in the output, set configuration parameter **"include_key"** to true. To exclude this index (key) from the output set **"include_key"** to false. The output column name for this key (index) is set using the configuration parameter **"column_name_for_key"**.
|
|
145
|
+
|
|
146
|
+
The **"columns"** sub-parameter of the **"main_line"** and **"linked_lines"** configuration parameters includes a dictionary from a string to a list of strings. The keys in this dictionary is the column names to use in the output. The list of strings is the relative path in the record of the item that has the value for this column.
|
|
147
|
+
|
|
148
|
+
Sometimes a single record in the input, defined as the item in the list or dictionary referenced by the **"line"** sub-parameter, can contain several sub-records. For instance if the record is a purchase order, then the order may include several purchased items. As the output format of a list of columns does not support such nesting, the single input redord needs to be split into several output lines. The common items in the input record is then duplicated on all such lines split from the same input record. The configuration sub-parameter **"expand_at"** holds a list of relative paths where the input record should be expanded (or split). Each such relative path is configured using a list of strings in the **"expand_at"** sub-parameter. As the **"expand_at"** holds a list of relative paths (list of list of strings), this expansion can be configured to be done on zero, one or several places in the input record.
|
|
149
|
+
|
|
150
|
+
There can be only one main line, denoted by the **"main_line"** configuration parameter. In contrast there may be any number of linked lines. The linked lines are described be an array for the the **"linked_lines"** configuration parameter. The sub-parameters described for the **"main_line"** shall also be configured for each item in the **"linked_lines"** array.
|
|
151
|
+
|
|
152
|
+
How linked lines are tied to main line is defined by the **"linked_column"** and **"linked_main_column"** sub-parameter for each linked line item. Both **"linked_column"** and **"linked_main_column"** are relative paths in the input records using the familiar list of strings syntax. The **"linked_column"** sub-parameter denotes an item in the linked line record that shall have the same value as the item in the main line record denoted by the the **"linked_main_column"** sub-parameter.
|
|
153
|
+
|
|
154
|
+
The data items used to link a linked line to a main line may be extracted to the output using the **"column"** sub-parameter, but this is totally optional. There is no requirement that the data items used to tie linked lines to main lines are part of the output.
|
|
155
|
+
|
|
156
|
+
Several linked lines could be tied to the same main line. The configuration parameter **"one_output_line_per_main_line"** determines how this case is handled. If it is set to false, the result will be that the main line part is duplicated so that the output has one line for each tied combination of main line and linked lines. Sometimes this duplication of main line is not intended. By setting the configuration parameter **"one_output_line_per_main_line"** to true, several linked lines tied to one main line will be flagged as an error.
|
|
157
|
+
|
|
158
|
+
As items in records are optional in the input formats (JSON and XML) it is possible the that there is no data at the paths specified for columns or specified for records. The configuration parameter **"missing_input_for_column"** determines how missing data in input is handled. The possible values are *"EMPTY"* and *"ERROR"*. If configured as *"EMPTY"* the columns resulting from the missing input data will simply be empty.
|
|
159
|
+
|
|
160
|
+
### XML attributes
|
|
161
|
+
|
|
162
|
+
The XML syntax allows member values of an object to either be written as nested objects or as attributes. If the input has XML attributes the key for the attributes will have an "@" prepended. To handle this the configuration parameter **"in_xml_strip_at"** can be set to *true* or *false*. If **"in_xml_strip_at"** is set to *true* an "@" character in the beginning of any key will be stripped off.
|
|
163
|
+
|
|
164
|
+
The configuration parameter **"out_xml_attributes"** specifies a list of column names. These columns will be written as XML attributes in XML output, not as nested objects.
|
|
165
|
+
|
|
166
|
+
### Output column order
|
|
167
|
+
|
|
168
|
+
The order of the columns in the output is specified with the configuration parameter **"column_order"**. The value of this parameter is a list of strings.
|
|
169
|
+
|
|
170
|
+
Specifying a column in the output column order that has not been extracted is an error. It is also an error to extract a column and not specify it in the output column order.
|
|
171
|
+
|
|
172
|
+
Output line order
|
|
173
|
+
|
|
174
|
+
The configuration parameter **"order_rows_by"** specifies that lines produced shall be sorted based on these columns. The most significant column shall be first in the list of column names.
|
|
175
|
+
|
|
176
|
+
The default order or lines produced is to order them based on the list of columns in the **"column_order"** configuration parameter. Leave **"order_rows_by"** as empty list unless you have a reason to request another specific order than the default.
|
|
177
|
+
|
|
178
|
+
## Source code
|
|
179
|
+
|
|
180
|
+
Source code and tests are available at [https://bitbucket.org/tom-bjorkholm/extract-list](https://bitbucket.org/tom-bjorkholm/extract-list).
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# extract-list
|
|
2
|
+
|
|
3
|
+
## Background
|
|
4
|
+
|
|
5
|
+
This python application was born out of the experience that needed data was available as part of JSON or as part of XML files, but the data was needed as a list of columns in excel or CSV (comma separated values) format.
|
|
6
|
+
|
|
7
|
+
## What it does
|
|
8
|
+
|
|
9
|
+
This small python application:
|
|
10
|
+
|
|
11
|
+
* reads data from an XML file or from a JSON file.
|
|
12
|
+
* extracts (a configurable part of the) data from the data read
|
|
13
|
+
* outputs the extracted data as list with a number of columns in the desired format that can be:
|
|
14
|
+
* Excel
|
|
15
|
+
* CSV (comma separated values)
|
|
16
|
+
* plain text file
|
|
17
|
+
* JSON
|
|
18
|
+
* XML
|
|
19
|
+
|
|
20
|
+
How this is done is governed by a configuration file. The application can create a number of example configuration files with accompanying description text files.
|
|
21
|
+
|
|
22
|
+
## Using it
|
|
23
|
+
|
|
24
|
+
If you want to use it install it using pip from [https://pypi.org/project/extract-list](https://pypi.org/project/extract-list). There is no need download anything from Bitbucket to use the application.
|
|
25
|
+
|
|
26
|
+
### Installing on mac and Linux
|
|
27
|
+
|
|
28
|
+
````sh
|
|
29
|
+
pip3 install extract-list
|
|
30
|
+
````
|
|
31
|
+
|
|
32
|
+
### Installing on Microsoft Windows
|
|
33
|
+
|
|
34
|
+
````sh
|
|
35
|
+
pip install extract-list
|
|
36
|
+
````
|
|
37
|
+
|
|
38
|
+
### Information for use
|
|
39
|
+
|
|
40
|
+
Please see [https://pypi.org/project/extract-list](https://pypi.org/project/extract-list) or please see README_pypi.md
|
|
41
|
+
|
|
42
|
+
## For developers
|
|
43
|
+
|
|
44
|
+
### Needed environment
|
|
45
|
+
|
|
46
|
+
#### OS
|
|
47
|
+
|
|
48
|
+
For running the script and running the test suite you need a mac or a Linux computer. Even if the resulting application can be installed and used on Windows, the scripts for building and testing is only implemented for mac and Linux.
|
|
49
|
+
|
|
50
|
+
#### Python version
|
|
51
|
+
|
|
52
|
+
The tests and the script for running the tests, coverage, mypy etc. requires Python version 3.12.6 or newer.
|
|
53
|
+
|
|
54
|
+
#### Zsh
|
|
55
|
+
|
|
56
|
+
The scripts are all zsh. zsh is available by default on modern macs. zsh can easily be installed on Linux (on Ubuntu: sudo apt install zsh).
|
|
57
|
+
|
|
58
|
+
### Internal APIs not guaranteed
|
|
59
|
+
|
|
60
|
+
The internal APIs in this package are not guaranteed to be stable. They can change without warning between versions.
|
|
61
|
+
|
|
62
|
+
### Building application
|
|
63
|
+
|
|
64
|
+
There are 3 scripts for building the application
|
|
65
|
+
|
|
66
|
+
* setup_build_environment.zsh
|
|
67
|
+
Run this script first to get the environment set up for building
|
|
68
|
+
* doBuild.zsh
|
|
69
|
+
Run this script to build an installation package (.whl) and to run the tests on it in a venv (virtual environment).
|
|
70
|
+
* clean.zsh
|
|
71
|
+
Deletes all files that was produced by the build to start over from a clean state.
|
|
72
|
+
|
|
73
|
+
The "testing" includes pytest, pylint, flake8 and mypy.
|
|
74
|
+
|
|
75
|
+
After running doBuild.zsh you can open reports/index.htm to see all test reports.
|
|
76
|
+
|
|
77
|
+
After running doBuild.zsh you can do manual test of the built and installed application in the virtual environment ./venv
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# extract-list
|
|
2
|
+
|
|
3
|
+
## Background
|
|
4
|
+
|
|
5
|
+
This python application was born out of the experience that needed data was available as part of JSON or as part of XML files, but the data was needed as a list of columns in excel or CSV (comma separated values) format.
|
|
6
|
+
|
|
7
|
+
## What it does
|
|
8
|
+
|
|
9
|
+
This small python application:
|
|
10
|
+
|
|
11
|
+
* reads data from an XML file or from a JSON file.
|
|
12
|
+
* extracts (a configurable part of the) data from the data read
|
|
13
|
+
* outputs the extracted data as list with a number of columns in the desired format that can be:
|
|
14
|
+
* Excel
|
|
15
|
+
* CSV (comma separated values)
|
|
16
|
+
* plain text file
|
|
17
|
+
* JSON
|
|
18
|
+
* XML
|
|
19
|
+
|
|
20
|
+
How this is done is governed by a configuration file. The application can create a number of example configuration files with accompanying description text files.
|
|
21
|
+
|
|
22
|
+
## Installing it
|
|
23
|
+
|
|
24
|
+
If you want to use it, install it using pip. A precondition is that you have Python 3.12.6 or newer installed on you computer. Python can be downloaded from [https://www.python.org/downloads/](https://www.python.org/downloads/).
|
|
25
|
+
|
|
26
|
+
### Installing on mac and Linux
|
|
27
|
+
|
|
28
|
+
````sh
|
|
29
|
+
pip3 install extract-list
|
|
30
|
+
````
|
|
31
|
+
|
|
32
|
+
### Installing on Microsoft Windows
|
|
33
|
+
|
|
34
|
+
````sh
|
|
35
|
+
pip install extract-list
|
|
36
|
+
````
|
|
37
|
+
|
|
38
|
+
## Running the application
|
|
39
|
+
|
|
40
|
+
### Running the application on mac and Linux
|
|
41
|
+
|
|
42
|
+
````sh
|
|
43
|
+
python3 -m extract_list --help
|
|
44
|
+
python3 -m extract_list cfg-example --help
|
|
45
|
+
python3 -m extract_list extract --help
|
|
46
|
+
python3 -m extract_list cfg-example -k sw_json_to_rrs -t excel -o sw-example.cfg
|
|
47
|
+
python3 -m extract_list extract -c sw-example.cfg -i input.json -o output.xlsx
|
|
48
|
+
````
|
|
49
|
+
|
|
50
|
+
### Running the application on Microsoft Windows
|
|
51
|
+
|
|
52
|
+
````sh
|
|
53
|
+
python -m extract_list --help
|
|
54
|
+
python -m extract_list cfg-example --help
|
|
55
|
+
python -m extract_list extract --help
|
|
56
|
+
python -m extract_list cfg-example -k sw_json_to_rrs -t excel -o sw-example.cfg
|
|
57
|
+
python -m extract_list extract -c sw-example.cfg -i input.json -o output.xlsx
|
|
58
|
+
````
|
|
59
|
+
|
|
60
|
+
## Suggested way to get started
|
|
61
|
+
|
|
62
|
+
1. Use the "cfg-example" sub-command to generate a few example configuration (.cfg) files with description (.txt) files.
|
|
63
|
+
2. Read the example configuration (.cfg) files and the accompanying description (.txt) files.
|
|
64
|
+
3. Find an example that is close to what you want to achieve.
|
|
65
|
+
4. Modify that configuration file to achieve what you want to achieve.
|
|
66
|
+
5. Use the "extract" sub-command to read your data and output extracted data according to your modified configuration file.
|
|
67
|
+
6. Read the produced output. If necessary go back to step 4 and adjust how the data is transformed.
|
|
68
|
+
|
|
69
|
+
### Example configuration files
|
|
70
|
+
|
|
71
|
+
When using the "cfg-example" sub-command to generate an example configuration file (say example.cfg) a text file describing the configuration and the syntax of the configuration file is also generated. If the example configuration file is named example.cfg, then the text file descriging the configuration is named example.txt.
|
|
72
|
+
|
|
73
|
+
You can generate several example configuration files each with an accompanying text file descriping it.
|
|
74
|
+
|
|
75
|
+
Read the text file describing the configuration file while looking at the configuration file to understand the syntax and the possible options.
|
|
76
|
+
|
|
77
|
+
## Description of how to write/change the configuration file
|
|
78
|
+
|
|
79
|
+
The configuration file is in JSON syntax [https://en.wikipedia.org/wiki/JSON](https://en.wikipedia.org/wiki/JSON).
|
|
80
|
+
The keywords and the nesting is important. The order of keywords have no significance (the examples use alphabetical order). Indentation and line breaks have no significance.
|
|
81
|
+
|
|
82
|
+
The encoding for the configuration file must be UTF-8. (US-ACII is a subset of UTF-8.)
|
|
83
|
+
|
|
84
|
+
It is recommended that you let the command generate a configuration file and then edit that file to match your needs. It is NOT recommended that the user writes the configuration file from scratch.
|
|
85
|
+
|
|
86
|
+
### Type of input file
|
|
87
|
+
|
|
88
|
+
The type of input file to read is determined by **"infile_type"**. **"infile_type"** can have the values:
|
|
89
|
+
|
|
90
|
+
* *"JSON"*
|
|
91
|
+
* *"XML"*
|
|
92
|
+
|
|
93
|
+
As both JSON and XML are syntax in text files, the text files can have an encoding for the text in the files
|
|
94
|
+
[https://en.wikipedia.org/wiki/Character_encoding](https://en.wikipedia.org/wiki/Character_encoding).
|
|
95
|
+
This is specified with **"infile_encoding"**. Unless you know that you need another encoding leave this as in the generated example configuration.
|
|
96
|
+
|
|
97
|
+
### Type of output file
|
|
98
|
+
|
|
99
|
+
The type of output file to produce is determined by **"outfile_type"**. **"outfile_type"** can have the values:
|
|
100
|
+
|
|
101
|
+
* *"EXCEL"*
|
|
102
|
+
* *"CSV"*
|
|
103
|
+
* *"JSON"*
|
|
104
|
+
* *"XML"*
|
|
105
|
+
* *"TXT"*
|
|
106
|
+
|
|
107
|
+
As *CSV, TXT, JSON* and *XML* are syntaxes in text files, the text files can have an encoding for the text in the files
|
|
108
|
+
[https://en.wikipedia.org/wiki/Character_encoding](https://en.wikipedia.org/wiki/Character_encoding).
|
|
109
|
+
This is specified with **"outfile_encoding"**. Unless you know that you need another encoding leave this as in the generated example configuration.
|
|
110
|
+
|
|
111
|
+
Comma separated values files (CSV files) may differ slightly depending on the programs used to read/write them and the locale used. **"out_csv_dialect"** changes how CSV files are written. It is always needed in the configuration file, but is only used if the output is CSV.
|
|
112
|
+
|
|
113
|
+
Excel files can be written using three libraries. **"outfile_excel_library"** can have values *"OPENPYXL"*, *"XLSXWRITER"* or *"PYLIGHTXL"*. These are different third party libraries that can read/write excel. My experience is that "PYLIGHTXL" most often is able to read and write excel files correctly. If you have trouble writing your particular excel file, please try another library. **"outfile_excel_library"** is always needed in the configuration file but is only used if the output is excel.
|
|
114
|
+
|
|
115
|
+
### Data to extract
|
|
116
|
+
|
|
117
|
+
The input files (JSON or XML) is likely to include a lot more data than what is interesting to extract. The data to extract is specified using **"main_line"** and **"linked_lines"** parameters in the configuration file.
|
|
118
|
+
|
|
119
|
+
The **"main_line"** specifies what part of the input file should be the main part of the output line(s). The linked lines have some data linking them to the main line: A linked line is linked to the main line if some item in the linked line has the same value as some other item in the main line.
|
|
120
|
+
|
|
121
|
+
The **"line"** sub-parameter of the the **"main_line"** and **"linked_lines"** configuration parameters includes a list of strings. This list of strings is the path of keywords to the records. Directly below this path is either a list or a dictionary of the records.
|
|
122
|
+
|
|
123
|
+
The dictionary of the records of the **"main_line"** are indexed in the input (either by an integer index in the case of a list, or by the key in case of a dictionary). If you want this index (also known as key) to be included in the output, set configuration parameter **"include_key"** to true. To exclude this index (key) from the output set **"include_key"** to false. The output column name for this key (index) is set using the configuration parameter **"column_name_for_key"**.
|
|
124
|
+
|
|
125
|
+
The **"columns"** sub-parameter of the **"main_line"** and **"linked_lines"** configuration parameters includes a dictionary from a string to a list of strings. The keys in this dictionary is the column names to use in the output. The list of strings is the relative path in the record of the item that has the value for this column.
|
|
126
|
+
|
|
127
|
+
Sometimes a single record in the input, defined as the item in the list or dictionary referenced by the **"line"** sub-parameter, can contain several sub-records. For instance if the record is a purchase order, then the order may include several purchased items. As the output format of a list of columns does not support such nesting, the single input redord needs to be split into several output lines. The common items in the input record is then duplicated on all such lines split from the same input record. The configuration sub-parameter **"expand_at"** holds a list of relative paths where the input record should be expanded (or split). Each such relative path is configured using a list of strings in the **"expand_at"** sub-parameter. As the **"expand_at"** holds a list of relative paths (list of list of strings), this expansion can be configured to be done on zero, one or several places in the input record.
|
|
128
|
+
|
|
129
|
+
There can be only one main line, denoted by the **"main_line"** configuration parameter. In contrast there may be any number of linked lines. The linked lines are described be an array for the the **"linked_lines"** configuration parameter. The sub-parameters described for the **"main_line"** shall also be configured for each item in the **"linked_lines"** array.
|
|
130
|
+
|
|
131
|
+
How linked lines are tied to main line is defined by the **"linked_column"** and **"linked_main_column"** sub-parameter for each linked line item. Both **"linked_column"** and **"linked_main_column"** are relative paths in the input records using the familiar list of strings syntax. The **"linked_column"** sub-parameter denotes an item in the linked line record that shall have the same value as the item in the main line record denoted by the the **"linked_main_column"** sub-parameter.
|
|
132
|
+
|
|
133
|
+
The data items used to link a linked line to a main line may be extracted to the output using the **"column"** sub-parameter, but this is totally optional. There is no requirement that the data items used to tie linked lines to main lines are part of the output.
|
|
134
|
+
|
|
135
|
+
Several linked lines could be tied to the same main line. The configuration parameter **"one_output_line_per_main_line"** determines how this case is handled. If it is set to false, the result will be that the main line part is duplicated so that the output has one line for each tied combination of main line and linked lines. Sometimes this duplication of main line is not intended. By setting the configuration parameter **"one_output_line_per_main_line"** to true, several linked lines tied to one main line will be flagged as an error.
|
|
136
|
+
|
|
137
|
+
As items in records are optional in the input formats (JSON and XML) it is possible the that there is no data at the paths specified for columns or specified for records. The configuration parameter **"missing_input_for_column"** determines how missing data in input is handled. The possible values are *"EMPTY"* and *"ERROR"*. If configured as *"EMPTY"* the columns resulting from the missing input data will simply be empty.
|
|
138
|
+
|
|
139
|
+
### XML attributes
|
|
140
|
+
|
|
141
|
+
The XML syntax allows member values of an object to either be written as nested objects or as attributes. If the input has XML attributes the key for the attributes will have an "@" prepended. To handle this the configuration parameter **"in_xml_strip_at"** can be set to *true* or *false*. If **"in_xml_strip_at"** is set to *true* an "@" character in the beginning of any key will be stripped off.
|
|
142
|
+
|
|
143
|
+
The configuration parameter **"out_xml_attributes"** specifies a list of column names. These columns will be written as XML attributes in XML output, not as nested objects.
|
|
144
|
+
|
|
145
|
+
### Output column order
|
|
146
|
+
|
|
147
|
+
The order of the columns in the output is specified with the configuration parameter **"column_order"**. The value of this parameter is a list of strings.
|
|
148
|
+
|
|
149
|
+
Specifying a column in the output column order that has not been extracted is an error. It is also an error to extract a column and not specify it in the output column order.
|
|
150
|
+
|
|
151
|
+
Output line order
|
|
152
|
+
|
|
153
|
+
The configuration parameter **"order_rows_by"** specifies that lines produced shall be sorted based on these columns. The most significant column shall be first in the list of column names.
|
|
154
|
+
|
|
155
|
+
The default order or lines produced is to order them based on the list of columns in the **"column_order"** configuration parameter. Leave **"order_rows_by"** as empty list unless you have a reason to request another specific order than the default.
|
|
156
|
+
|
|
157
|
+
## Source code
|
|
158
|
+
|
|
159
|
+
Source code and tests are available at [https://bitbucket.org/tom-bjorkholm/extract-list](https://bitbucket.org/tom-bjorkholm/extract-list).
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: extract-list
|
|
3
|
+
Version: 0.2
|
|
4
|
+
Summary: Extract a list from JSON or XML, save to excel, csv, etc.
|
|
5
|
+
Author: Tom Björkholm
|
|
6
|
+
Author-email: Tom Björkholm <klausuler_linnet0q@icloud.com>
|
|
7
|
+
Project-URL: Source code, https://bitbucket.org/tom-bjorkholm/extract-list
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.12.6
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE.txt
|
|
14
|
+
Requires-Dist: excel-list-transform>=0.7.1
|
|
15
|
+
Requires-Dist: xmltodict>=0.13.0
|
|
16
|
+
Requires-Dist: types-xmltodict>=0.13.0.3
|
|
17
|
+
Requires-Dist: pip>=24.2
|
|
18
|
+
Requires-Dist: setuptools>=75.6.0
|
|
19
|
+
Requires-Dist: build>=1.2.2
|
|
20
|
+
Requires-Dist: wheel>=0.45.1
|
|
21
|
+
|
|
22
|
+
# extract-list
|
|
23
|
+
|
|
24
|
+
## Background
|
|
25
|
+
|
|
26
|
+
This python application was born out of the experience that needed data was available as part of JSON or as part of XML files, but the data was needed as a list of columns in excel or CSV (comma separated values) format.
|
|
27
|
+
|
|
28
|
+
## What it does
|
|
29
|
+
|
|
30
|
+
This small python application:
|
|
31
|
+
|
|
32
|
+
* reads data from an XML file or from a JSON file.
|
|
33
|
+
* extracts (a configurable part of the) data from the data read
|
|
34
|
+
* outputs the extracted data as list with a number of columns in the desired format that can be:
|
|
35
|
+
* Excel
|
|
36
|
+
* CSV (comma separated values)
|
|
37
|
+
* plain text file
|
|
38
|
+
* JSON
|
|
39
|
+
* XML
|
|
40
|
+
|
|
41
|
+
How this is done is governed by a configuration file. The application can create a number of example configuration files with accompanying description text files.
|
|
42
|
+
|
|
43
|
+
## Installing it
|
|
44
|
+
|
|
45
|
+
If you want to use it, install it using pip. A precondition is that you have Python 3.12.6 or newer installed on you computer. Python can be downloaded from [https://www.python.org/downloads/](https://www.python.org/downloads/).
|
|
46
|
+
|
|
47
|
+
### Installing on mac and Linux
|
|
48
|
+
|
|
49
|
+
````sh
|
|
50
|
+
pip3 install extract-list
|
|
51
|
+
````
|
|
52
|
+
|
|
53
|
+
### Installing on Microsoft Windows
|
|
54
|
+
|
|
55
|
+
````sh
|
|
56
|
+
pip install extract-list
|
|
57
|
+
````
|
|
58
|
+
|
|
59
|
+
## Running the application
|
|
60
|
+
|
|
61
|
+
### Running the application on mac and Linux
|
|
62
|
+
|
|
63
|
+
````sh
|
|
64
|
+
python3 -m extract_list --help
|
|
65
|
+
python3 -m extract_list cfg-example --help
|
|
66
|
+
python3 -m extract_list extract --help
|
|
67
|
+
python3 -m extract_list cfg-example -k sw_json_to_rrs -t excel -o sw-example.cfg
|
|
68
|
+
python3 -m extract_list extract -c sw-example.cfg -i input.json -o output.xlsx
|
|
69
|
+
````
|
|
70
|
+
|
|
71
|
+
### Running the application on Microsoft Windows
|
|
72
|
+
|
|
73
|
+
````sh
|
|
74
|
+
python -m extract_list --help
|
|
75
|
+
python -m extract_list cfg-example --help
|
|
76
|
+
python -m extract_list extract --help
|
|
77
|
+
python -m extract_list cfg-example -k sw_json_to_rrs -t excel -o sw-example.cfg
|
|
78
|
+
python -m extract_list extract -c sw-example.cfg -i input.json -o output.xlsx
|
|
79
|
+
````
|
|
80
|
+
|
|
81
|
+
## Suggested way to get started
|
|
82
|
+
|
|
83
|
+
1. Use the "cfg-example" sub-command to generate a few example configuration (.cfg) files with description (.txt) files.
|
|
84
|
+
2. Read the example configuration (.cfg) files and the accompanying description (.txt) files.
|
|
85
|
+
3. Find an example that is close to what you want to achieve.
|
|
86
|
+
4. Modify that configuration file to achieve what you want to achieve.
|
|
87
|
+
5. Use the "extract" sub-command to read your data and output extracted data according to your modified configuration file.
|
|
88
|
+
6. Read the produced output. If necessary go back to step 4 and adjust how the data is transformed.
|
|
89
|
+
|
|
90
|
+
### Example configuration files
|
|
91
|
+
|
|
92
|
+
When using the "cfg-example" sub-command to generate an example configuration file (say example.cfg) a text file describing the configuration and the syntax of the configuration file is also generated. If the example configuration file is named example.cfg, then the text file descriging the configuration is named example.txt.
|
|
93
|
+
|
|
94
|
+
You can generate several example configuration files each with an accompanying text file descriping it.
|
|
95
|
+
|
|
96
|
+
Read the text file describing the configuration file while looking at the configuration file to understand the syntax and the possible options.
|
|
97
|
+
|
|
98
|
+
## Description of how to write/change the configuration file
|
|
99
|
+
|
|
100
|
+
The configuration file is in JSON syntax [https://en.wikipedia.org/wiki/JSON](https://en.wikipedia.org/wiki/JSON).
|
|
101
|
+
The keywords and the nesting is important. The order of keywords have no significance (the examples use alphabetical order). Indentation and line breaks have no significance.
|
|
102
|
+
|
|
103
|
+
The encoding for the configuration file must be UTF-8. (US-ACII is a subset of UTF-8.)
|
|
104
|
+
|
|
105
|
+
It is recommended that you let the command generate a configuration file and then edit that file to match your needs. It is NOT recommended that the user writes the configuration file from scratch.
|
|
106
|
+
|
|
107
|
+
### Type of input file
|
|
108
|
+
|
|
109
|
+
The type of input file to read is determined by **"infile_type"**. **"infile_type"** can have the values:
|
|
110
|
+
|
|
111
|
+
* *"JSON"*
|
|
112
|
+
* *"XML"*
|
|
113
|
+
|
|
114
|
+
As both JSON and XML are syntax in text files, the text files can have an encoding for the text in the files
|
|
115
|
+
[https://en.wikipedia.org/wiki/Character_encoding](https://en.wikipedia.org/wiki/Character_encoding).
|
|
116
|
+
This is specified with **"infile_encoding"**. Unless you know that you need another encoding leave this as in the generated example configuration.
|
|
117
|
+
|
|
118
|
+
### Type of output file
|
|
119
|
+
|
|
120
|
+
The type of output file to produce is determined by **"outfile_type"**. **"outfile_type"** can have the values:
|
|
121
|
+
|
|
122
|
+
* *"EXCEL"*
|
|
123
|
+
* *"CSV"*
|
|
124
|
+
* *"JSON"*
|
|
125
|
+
* *"XML"*
|
|
126
|
+
* *"TXT"*
|
|
127
|
+
|
|
128
|
+
As *CSV, TXT, JSON* and *XML* are syntaxes in text files, the text files can have an encoding for the text in the files
|
|
129
|
+
[https://en.wikipedia.org/wiki/Character_encoding](https://en.wikipedia.org/wiki/Character_encoding).
|
|
130
|
+
This is specified with **"outfile_encoding"**. Unless you know that you need another encoding leave this as in the generated example configuration.
|
|
131
|
+
|
|
132
|
+
Comma separated values files (CSV files) may differ slightly depending on the programs used to read/write them and the locale used. **"out_csv_dialect"** changes how CSV files are written. It is always needed in the configuration file, but is only used if the output is CSV.
|
|
133
|
+
|
|
134
|
+
Excel files can be written using three libraries. **"outfile_excel_library"** can have values *"OPENPYXL"*, *"XLSXWRITER"* or *"PYLIGHTXL"*. These are different third party libraries that can read/write excel. My experience is that "PYLIGHTXL" most often is able to read and write excel files correctly. If you have trouble writing your particular excel file, please try another library. **"outfile_excel_library"** is always needed in the configuration file but is only used if the output is excel.
|
|
135
|
+
|
|
136
|
+
### Data to extract
|
|
137
|
+
|
|
138
|
+
The input files (JSON or XML) is likely to include a lot more data than what is interesting to extract. The data to extract is specified using **"main_line"** and **"linked_lines"** parameters in the configuration file.
|
|
139
|
+
|
|
140
|
+
The **"main_line"** specifies what part of the input file should be the main part of the output line(s). The linked lines have some data linking them to the main line: A linked line is linked to the main line if some item in the linked line has the same value as some other item in the main line.
|
|
141
|
+
|
|
142
|
+
The **"line"** sub-parameter of the the **"main_line"** and **"linked_lines"** configuration parameters includes a list of strings. This list of strings is the path of keywords to the records. Directly below this path is either a list or a dictionary of the records.
|
|
143
|
+
|
|
144
|
+
The dictionary of the records of the **"main_line"** are indexed in the input (either by an integer index in the case of a list, or by the key in case of a dictionary). If you want this index (also known as key) to be included in the output, set configuration parameter **"include_key"** to true. To exclude this index (key) from the output set **"include_key"** to false. The output column name for this key (index) is set using the configuration parameter **"column_name_for_key"**.
|
|
145
|
+
|
|
146
|
+
The **"columns"** sub-parameter of the **"main_line"** and **"linked_lines"** configuration parameters includes a dictionary from a string to a list of strings. The keys in this dictionary is the column names to use in the output. The list of strings is the relative path in the record of the item that has the value for this column.
|
|
147
|
+
|
|
148
|
+
Sometimes a single record in the input, defined as the item in the list or dictionary referenced by the **"line"** sub-parameter, can contain several sub-records. For instance if the record is a purchase order, then the order may include several purchased items. As the output format of a list of columns does not support such nesting, the single input redord needs to be split into several output lines. The common items in the input record is then duplicated on all such lines split from the same input record. The configuration sub-parameter **"expand_at"** holds a list of relative paths where the input record should be expanded (or split). Each such relative path is configured using a list of strings in the **"expand_at"** sub-parameter. As the **"expand_at"** holds a list of relative paths (list of list of strings), this expansion can be configured to be done on zero, one or several places in the input record.
|
|
149
|
+
|
|
150
|
+
There can be only one main line, denoted by the **"main_line"** configuration parameter. In contrast there may be any number of linked lines. The linked lines are described be an array for the the **"linked_lines"** configuration parameter. The sub-parameters described for the **"main_line"** shall also be configured for each item in the **"linked_lines"** array.
|
|
151
|
+
|
|
152
|
+
How linked lines are tied to main line is defined by the **"linked_column"** and **"linked_main_column"** sub-parameter for each linked line item. Both **"linked_column"** and **"linked_main_column"** are relative paths in the input records using the familiar list of strings syntax. The **"linked_column"** sub-parameter denotes an item in the linked line record that shall have the same value as the item in the main line record denoted by the the **"linked_main_column"** sub-parameter.
|
|
153
|
+
|
|
154
|
+
The data items used to link a linked line to a main line may be extracted to the output using the **"column"** sub-parameter, but this is totally optional. There is no requirement that the data items used to tie linked lines to main lines are part of the output.
|
|
155
|
+
|
|
156
|
+
Several linked lines could be tied to the same main line. The configuration parameter **"one_output_line_per_main_line"** determines how this case is handled. If it is set to false, the result will be that the main line part is duplicated so that the output has one line for each tied combination of main line and linked lines. Sometimes this duplication of main line is not intended. By setting the configuration parameter **"one_output_line_per_main_line"** to true, several linked lines tied to one main line will be flagged as an error.
|
|
157
|
+
|
|
158
|
+
As items in records are optional in the input formats (JSON and XML) it is possible the that there is no data at the paths specified for columns or specified for records. The configuration parameter **"missing_input_for_column"** determines how missing data in input is handled. The possible values are *"EMPTY"* and *"ERROR"*. If configured as *"EMPTY"* the columns resulting from the missing input data will simply be empty.
|
|
159
|
+
|
|
160
|
+
### XML attributes
|
|
161
|
+
|
|
162
|
+
The XML syntax allows member values of an object to either be written as nested objects or as attributes. If the input has XML attributes the key for the attributes will have an "@" prepended. To handle this the configuration parameter **"in_xml_strip_at"** can be set to *true* or *false*. If **"in_xml_strip_at"** is set to *true* an "@" character in the beginning of any key will be stripped off.
|
|
163
|
+
|
|
164
|
+
The configuration parameter **"out_xml_attributes"** specifies a list of column names. These columns will be written as XML attributes in XML output, not as nested objects.
|
|
165
|
+
|
|
166
|
+
### Output column order
|
|
167
|
+
|
|
168
|
+
The order of the columns in the output is specified with the configuration parameter **"column_order"**. The value of this parameter is a list of strings.
|
|
169
|
+
|
|
170
|
+
Specifying a column in the output column order that has not been extracted is an error. It is also an error to extract a column and not specify it in the output column order.
|
|
171
|
+
|
|
172
|
+
Output line order
|
|
173
|
+
|
|
174
|
+
The configuration parameter **"order_rows_by"** specifies that lines produced shall be sorted based on these columns. The most significant column shall be first in the list of column names.
|
|
175
|
+
|
|
176
|
+
The default order or lines produced is to order them based on the list of columns in the **"column_order"** configuration parameter. Leave **"order_rows_by"** as empty list unless you have a reason to request another specific order than the default.
|
|
177
|
+
|
|
178
|
+
## Source code
|
|
179
|
+
|
|
180
|
+
Source code and tests are available at [https://bitbucket.org/tom-bjorkholm/extract-list](https://bitbucket.org/tom-bjorkholm/extract-list).
|