aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThéo de la Hogue2024-07-18 22:51:10 +0200
committerThéo de la Hogue2024-07-18 22:51:10 +0200
commit3a96c7bb4c0692d83084e234af588991639c084f (patch)
tree0a96be2334379cb70d09360636d4484a3672cc7f
parenta8f92d1e670cfa3231e3d1315d6b6cb98ee4c2cc (diff)
downloadargaze-3a96c7bb4c0692d83084e234af588991639c084f.zip
argaze-3a96c7bb4c0692d83084e234af588991639c084f.tar.gz
argaze-3a96c7bb4c0692d83084e234af588991639c084f.tar.bz2
argaze-3a96c7bb4c0692d83084e234af588991639c084f.tar.xz
Writing data using csv.writer support.
-rw-r--r--src/argaze/utils/UtilsFeatures.py35
1 files changed, 7 insertions, 28 deletions
diff --git a/src/argaze/utils/UtilsFeatures.py b/src/argaze/utils/UtilsFeatures.py
index cac186a..aaa337b 100644
--- a/src/argaze/utils/UtilsFeatures.py
+++ b/src/argaze/utils/UtilsFeatures.py
@@ -155,11 +155,6 @@ class TimeProbe():
self.start()
-def tuple_to_string(t: tuple, separator: str = ", ") -> str:
- """Convert tuple elements into quoted strings separated by a separator string."""
-
- return separator.join(f'\"{e}\"' for e in t)
-
def PrintCallStack(method):
"""Define a decorator to print call stack until the decorated method."""
@@ -230,41 +225,29 @@ class FileWriter(DataFeatures.PipelineStepObject):
os.makedirs(self.__path.parent.absolute())
# Open file
- self.__file = open(self.__path, 'w', encoding='utf-8', buffering=1)
+ self.__file = open(self.__path, 'w', encoding='utf-8', newline='', buffering=1)
+ self.__writer = csv.writer(self.__file, delimiter=self.__separator, quoting=csv.QUOTE_NONNUMERIC)
# Write header if required
if self.__header is not None:
- # Format list or tuple element into quoted strings
- if not isinstance(self.__header, str):
-
- self.__header = tuple_to_string(self.__header, self.__separator)
-
- print(self.__header, file=self.__file, flush=True)
+ self.__writer.writerow(self.__header)
@DataFeatures.PipelineStepExit
def __exit__(self, exception_type, exception_value, exception_traceback):
"""Close file."""
+
self.__file.close()
def write(self, data: str|tuple):
- """Write data as a new line into file.
-
- !!! note
- Tuple elements are converted into quoted strings separated by separator string.
- """
+ """Write data as a new line into file."""
if self.__file.closed:
return
- # Format list or tuple element into quoted strings
- if not isinstance(data, str):
-
- data = tuple_to_string(data, self.__separator)
-
# Write into file
- print(data, file=self.__file, flush=True)
+ self.__writer.writerow(data)
class FileReader(DataFeatures.PipelineStepObject):
"""Read data from a file line by line."""
@@ -309,11 +292,7 @@ class FileReader(DataFeatures.PipelineStepObject):
pass
def read(self) -> str|tuple:
- """Read next data from file.
-
- !!! note
- Quoted strings separated by separator string are converted into tuple elements.
- """
+ """Read next data from file."""
try: