-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanup.py
86 lines (78 loc) · 2.56 KB
/
cleanup.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
# open the relevant files
#------------------------------------------------------------------------
# !!!!! CHANGE FILE NAME !!!!!!
#------------------------------------------------------------------------
f = open("haushalt6_clean.csv", "w+")
t = open("haushalt6.csv", "r")
#------------------------------------------------------------------------
# Creates one line of text
#------------------------------------------------------------------------
def textbau(l):
l = l.strip("\"") # delete "-symbols
l = l.split(",") # create list l from ","
s = l[-1].split(" ") # create sublist s from last list l item
if len(s) == 2: # if sublist s has 2 items
pass
else:
more_spaces(s, l) # start function more_spaces(s) if more items than 2
l[-1] = s[0]
l.append(s[-1])
#print("Davor ", l)
l = luckenschliesser(l)
#print("Fertig ", l)
if l[0] == "":
del l[0]
tmp = ""
for item in l:
tmp = tmp + "," + item
#print(tmp)
return tmp
#------------------------------------------------------------------------
# Deletes spaces in cells
#------------------------------------------------------------------------
def luckenschliesser(zellen):
zellen = zellen
ind = 0
for zelle in zellen:
if zelle[:3].isalpha():
#print(zelle)
pass
else:
z_new = ""
for z in zelle:
if z == " ":
pass
else:
z_new = z_new + z
zellen[ind] = z_new
ind += 1
l = zellen
#print("Inzwischen ", l)
return l
#------------------------------------------------------------------------
# In case there are more spaces in the last row
#------------------------------------------------------------------------
def more_spaces(s, l):
count = len(s)
l[-1] = s[0]
for index in range(1, count): # ERROR! Cullumn gets copied twice for some reason
l.append(s[index]) # or appended twice... In very rare cases though.
return l
text = ""
for l in t:
if l[:2] == "\"\"":
if l[:8] == "\"\",Summe":
text = text + textbau(l)
else:
pass
elif l[:2] == "Nr":
pass
elif l[:3] == "1,2":
pass
else:
text = text + textbau(l)
# write in "haushalt1_clean.csv"
f.write(text)
# close files
t.close()
f.close()