-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvoutput.py
96 lines (70 loc) · 2.74 KB
/
csvoutput.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python
# Copyright 2024 Martin Junius
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Usage
# from csvoutput import CSVOutput
# CSVOutput.set_default_locale(locale="")
# CSVOutput.set_float_format(fmt="%.3f")
# CSVOutput.add_row(list)
# CSVOutput.add_fields(list)
# CSVOutput.write(file="", set_locale=True) file="" uses stdout
# ChangeLog
# Version 1.0 / 2024-07-12
# locale aware global CSV output class
import csv
import locale
import sys
VERSION = "1.0 / 2024-07-12"
AUTHOR = "Martin Junius"
NAME = "csvoutput"
DEFAULT_FLOAT_FORMAT = "%.3f"
class CSVOutput:
_cache = []
_fields = None
_float_fmt = None # format for floats if set
def set_default_locale(loc=""):
locale.setlocale(locale.LC_ALL, loc)
if locale.localeconv()['decimal_point'] == ",":
# Set float format to automatically format all float values as strings using locale
if not CSVOutput._float_fmt:
CSVOutput.set_float_format()
def set_float_format(fmt=DEFAULT_FLOAT_FORMAT):
CSVOutput._float_fmt = fmt
def _fmt(v):
return locale.format_string(CSVOutput._float_fmt, v)
def add_row(data):
CSVOutput._cache.append(data)
def add_fields(fields):
CSVOutput._fields = fields
def _write(f):
if locale.localeconv()['decimal_point'] == ",":
# Use ; as the separator and quote all fields for easy import in "German" Excel
writer = csv.writer(f, dialect="excel", delimiter=";", quoting=csv.QUOTE_ALL)
else:
writer = csv.writer(f, dialect="excel")
if CSVOutput._fields:
writer.writerow(CSVOutput._fields)
# Write rows one by one the enable conversion of float values
for row in CSVOutput._cache:
if CSVOutput._float_fmt:
row = [ CSVOutput._fmt(v) if type(v) == float else v for v in row ]
writer.writerow(row)
def write(file=None, set_locale=True):
if set_locale:
CSVOutput.set_default_locale()
if file:
with open(file, 'w', newline='', encoding="utf-8") as f:
CSVOutput._write(f)
else:
CSVOutput._write(sys.stdout)