-
Notifications
You must be signed in to change notification settings - Fork 0
/
adif.py
145 lines (117 loc) · 4.57 KB
/
adif.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
##########CONVERT ADI HAM RADIO LOG FILES FROM QRZ TO CSV##########
import csv
import re
from datetime import date
#for prorgess bar
import sys
import os
import time
############GET FILE DATA############
#enter filename/location: C:Temp\[email protected]
print("Example: C:\Temp\[email protected]")
importFile = input('Enter location of the adi file:')
adif = open(importFile, 'r')
logLines = adif.readlines()
##########SETUP LISTS AND NAME OF FILE TO SAVE##########
##########SAVES IN SAME FOLDER AS THIS CODE##########
header = []
data = []
headerRow = []
dataRow = []
tempLines = []
tempQso = []
writeTo = "ADIF_to_CSV_" + str(date.today())
print("Saving file to: " + writeTo + ".csv")
writeTo = writeTo + ".csv"
##########START THE BUILD##########
#open the file
f = open(writeTo, 'w', newline='')
writer = csv.writer(f)
#do a quick cleanup of the log and figure out which line to start after header info,
for x in range(len(logLines)):
logLines[x] = logLines[x].strip()
logLines[x] = logLines[x].replace("\t", "")
logLines[x] = logLines[x].replace("\n", "")
#Go through all lines, and slit/append new ones where multiple tags found on one line.
#show progress
animation = ["[■□□□□□□□□□]","[■■□□□□□□□□]", "[■■■□□□□□□□]", "[■■■■□□□□□□]", "[■■■■■□□□□□]", "[■■■■■■□□□□]", "[■■■■■■■□□□]", "[■■■■■■■■□□]", "[■■■■■■■■■□]", "[■■■■■■■■■■]"]
# for i in range(len(animation)):
# time.sleep(0.2)
# sys.stdout.write("\r" + animation[i % len(animation)])
# sys.stdout.flush()
# print("\n")
print("Loading:")
for x in range(len(logLines)):
#add your own fields in comments or notes by using (()) to create the field
logLines[x] = logLines[x].replace("((", "Xtra Field <")
logLines[x] = logLines[x].replace("))", ":>")
#find the tags
findTags = re.findall('<[^<]*?>', logLines[x])
for y in range(len(findTags)):
logLines[x] = logLines[x].replace(findTags[y], "<<" + findTags[y])
newLines = logLines[x].split("<<")
if len(newLines) > 0:
for z in range(len(newLines)):
tempLines.append(newLines[z])
sys.stdout.write("\r" + animation[int(x/8000) % len(animation)])
sys.stdout.flush()
logLines = tempLines
while("" in logLines):
logLines.remove("")
#find end of header
fstart = 0
for x in range(len(logLines)):
test = (str(logLines[x])).lower().find("<eoh>")
if test != -1:
fstart = x + 1
x = len(logLines) + 1
#build header (aka first row) and cycle through all the lines to make sure all fields are picked up
for x in range(fstart, len(logLines)):
if (str(logLines[x])).lower() != "<eor>":
logLines[x] = logLines[x].split(":")
logLines[x][0] = logLines[x][0].replace("<", "")
if logLines[x][0] not in headerRow:
headerRow.append(logLines[x][0])
sys.stdout.write("\r" + animation[int(x/4000) % len(animation)])
sys.stdout.flush()
headerRow.sort()
header.append(headerRow)
#build rows, and add to data to matching column
#row needs to be sorted to match header
tempQso = headerRow.copy()
for x in range(fstart, len(logLines)):
if (str(logLines[x])).lower() != "<eor>":
tester = 0
#get rows in qso, sort in a tempQso
for z in range(len(tempQso)):
test = str(logLines[x]).find(str(tempQso[z]))
if test == 2:
tempQso[z] = str(logLines[x])
if (str(logLines[x])).lower() == "<eor>":
#append to dataRow
for y in range(len(tempQso)):
qsoData = ""
qsoData = qsoData + str(tempQso[y]).split(">")[-1]
qsoData = qsoData.replace("']", "")
dataRow.append(str(qsoData))
# print(dataRow)
# print("______________________")
data.append(dataRow)
dataRow = []
tempQso = headerRow.copy()
print("\n")
print("Rows added:")
print(len(data))
#write the header
writer.writerow(header[0])
#write the data
for x in range(len(data)):
writer.writerow(data[x])
# close the file
f.close()
# give user time to close:
print("Saved file as: " + writeTo)
k=input("Press 'Enter' to open file and exit this applicaton")
dir_path = os.path.dirname(os.path.realpath(__file__))
os.startfile(writeTo)
##########TADA##########