|
@@ -1,4 +1,5 @@
|
|
|
import csv
|
|
|
+import re
|
|
|
from pathlib import Path
|
|
|
from itertools import chain
|
|
|
|
|
@@ -24,6 +25,7 @@ class IFTConverter:
|
|
|
'010': self.import_config(base_dir + 'plan_line.txt')
|
|
|
}
|
|
|
}
|
|
|
+ self.is_number = re.compile(r'\d+\.\d+\-?$')
|
|
|
|
|
|
def import_config(self, filename):
|
|
|
with open(filename, 'r') as frh:
|
|
@@ -33,21 +35,26 @@ class IFTConverter:
|
|
|
|
|
|
def convert_dir(self, path):
|
|
|
source_path = Path(path)
|
|
|
- target_path = source_path.parent.joinpath('prod_csv')
|
|
|
+ target_path = source_path.parent.joinpath('staging')
|
|
|
|
|
|
- for filename in source_path.glob('*.txt'):
|
|
|
+ for filename in source_path.glob('*'):
|
|
|
print(filename.name)
|
|
|
- self.convert_file(filename, target_path)
|
|
|
+ if filename.name.count('_') < 2:
|
|
|
+ print('-> wrong file format')
|
|
|
+ elif filename.stat().st_size == 0:
|
|
|
+ print('-> file is empty!')
|
|
|
+ else:
|
|
|
+ self.convert_file(filename, target_path)
|
|
|
|
|
|
def convert_file(self, source, target_path):
|
|
|
cfg = self.conversion_config(source.name)
|
|
|
- target = target_path.joinpath(source.name[:-4] + '.csv')
|
|
|
+ target = target_path.joinpath(source.name + '.csv')
|
|
|
|
|
|
content = {}
|
|
|
last_key = list(cfg.keys())[-1]
|
|
|
|
|
|
- with open(source, 'r', errors='ignore') as frh:
|
|
|
- with open(target, 'w') as fwh:
|
|
|
+ with open(source, 'r', encoding='utf-8', errors='ignore') as frh:
|
|
|
+ with open(target, 'w', encoding='utf-8') as fwh:
|
|
|
for line in frh.readlines():
|
|
|
for key, rules in cfg.items():
|
|
|
if line.startswith(key):
|
|
@@ -57,7 +64,15 @@ class IFTConverter:
|
|
|
fwh.write('\t'.join(chain(*content.values())) + '\n')
|
|
|
|
|
|
def convert_line(self, line, rules):
|
|
|
- return [line[rule[0]:rule[1]].strip() for rule in rules]
|
|
|
+ return [self.convert_field(line, rule) for rule in rules]
|
|
|
+
|
|
|
+ def convert_field(self, line, rule):
|
|
|
+ field = line[rule[0]:rule[1]].strip()
|
|
|
+ if self.is_number.search(field):
|
|
|
+ field = field.replace(',', '')
|
|
|
+ if field[-1] == '-':
|
|
|
+ field = '-' + field[:-1]
|
|
|
+ return field
|
|
|
|
|
|
def conversion_config(self, filename):
|
|
|
for key, cfg in self.config.items():
|
|
@@ -69,8 +84,8 @@ class IFTConverter:
|
|
|
|
|
|
|
|
|
def main():
|
|
|
- ift_conv = IFTConverter('/home/robert/projekte/python/dbtools/config/IFT/')
|
|
|
- ift_conv.convert_dir('/home/robert/projekte/python/BMW_Wien/IFT/prod')
|
|
|
+ ift_conv = IFTConverter('E:\\GlobalCube\\Tasks\\Import\\config\\IFT\\')
|
|
|
+ ift_conv.convert_dir('E:\\IFT\\prod')
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|