forked from trockenasche/fdf2csv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfdf2csv.py
More file actions
61 lines (49 loc) · 1.57 KB
/
fdf2csv.py
File metadata and controls
61 lines (49 loc) · 1.57 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
61
#!/usr/bin/python
#title :fdf2csv.py
#description :Extract all data from FDF file to a CSV file
#author :trockenasche
#version :0.5.1
#usage :python fdf2csv.py file.fdf
#=================================================
import sys
import os
import re
import csv
# check if there are a argument
arglen = len(sys.argv)
if not arglen == 2:
print("Usage: fdf2csv.py file.fdf")
sys.exit()
# check if the file exist
fname = sys.argv[1]
if not os.path.isfile(fname):
print("Error: " + fname + " doesn't exist")
sys.exit()
# open file
fdf_file = open(sys.argv[1], "r")
fdf = fdf_file.read()
# replace "empty" String to an empty value
fdf_list = re.sub("(þÿ|FEFF)", "", fdf)
# print(fdf_list)
# Where the magic happened
pattern = re.compile('\/T\(([^)]*)\)\/V[(/<]([^>)]*)')
fdf_list = re.findall(pattern, fdf_list)
# print(fdf_list)
# separate head and values
csv_head = []
csv_values = []
for i in fdf_list:
csv_head.append(i[0])
csv_values.append(i[1])
# alternative way >>> csv_head, csv_values = zip(*fdf_list)
# Set the output filename based on input file
csv_file = re.sub("\.fdf", ".csv", fname)
print("writing file", csv_file)
with open(csv_file, "w") as myfile:
wr = csv.writer(myfile, delimiter=";", lineterminator='\n')
wr.writerow(csv_head)
wr.writerow(csv_values)
# TODO possibility to pass an alternative csv file as an argument
# TODO a possibility to get all fdf from the current folder
# TODO sorting the csv_head before
# TODO check if there already a csv file with the same header and append the values