-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
60 lines (52 loc) · 2.17 KB
/
Copy pathmain.py
File metadata and controls
60 lines (52 loc) · 2.17 KB
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
"""
Task 1
Read data in the provided file, modify and output the modified data
Requirements:
1. Load and read the file (from the file, do not put data into a variable)
2. Sort the data by the customer number
3. Update the modified date to the current date
4. Change the format of created date to UTC
5. Return the data as string
6. Provide option to output the data as HTML
"""
from datetime import datetime
from helpers import clean_cust_no, format_date_to_utc, convert_data_to_string, convert_data_to_html
# Regex pattern where we search for substring starting with a capital letter followed by
# one or more number or capital letter
cust_no_pattern = '[A-Z][\dA-Z]+'
# Known possible misspellings in the customer number int part
cust_no_int_fixes = [('O', '0'), ]
# Default datetime format
datetime_format = '%d/%m/%Y %H:%M'
def main() -> None:
# Ask the user for file path
file_path = input("Enter file path. If none given default 'example.txt' will be used: ")
if not file_path:
file_path = './example.txt'
# Ask the user for output format
html = True if input("Do you wish to return the output as HTML? (Y/N): ").capitalize() == "Y" else False
columns = None
content = list()
now = datetime.now().strftime(datetime_format)
with open(file_path) as input_file:
for row_number, row_content in enumerate(input_file):
row = [element.strip() for element in row_content.split('\t')]
if row_number == 0:
columns = row
else:
row[0] = clean_cust_no(row[0], cust_no_pattern, cust_no_int_fixes)
row[5] = format_date_to_utc(row[5], datetime_format)
row[6] = now
content.append(row)
# Sort list by customer number: first letter then number
content.sort(key=lambda x: (x[0][0], int(x[0][1:])))
if html:
print(convert_data_to_html(columns, content))
# return convert_data_to_html(columns, content)
else:
# Add column names to the rest of data
content.insert(0, columns)
print(convert_data_to_string(content))
# return convert_data_to_string(content)
if __name__ == '__main__':
main()