-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelimiter.py
More file actions
35 lines (26 loc) · 1.03 KB
/
Copy pathdelimiter.py
File metadata and controls
35 lines (26 loc) · 1.03 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
import csv
import os
import re
def delimitFile(newDirectory, filename, delimiter, fileOutputName):
# get the current working directory
current_working_directory = os.getcwd()
# print output to the console
print(current_working_directory)
# Change the working directory
os.chdir(newDirectory)
# Verify that the directory has changed
print("New working directory:", os.getcwd())
try:
with open(filename, 'r', encoding='latin-1') as infile, open(fileOutputName, 'w', newline='') as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile, delimiter=delimiter)
content = infile.read()
trimmed_content = content[2:]
new_text = re.sub(r"\s+", delimiter, trimmed_content)
split = new_text.split(delimiter)
final_merge = "\n".join([delimiter.join(split[i:i+3]) for i in range(0,len(split),3)])
outfile.write(final_merge)
except Exception as e:
print("Error Occurred: ")
print(e)
#