i got a file like this:
a;a_desc
b;b_desc
c;
d
;
e;e_desc
What i want is:
- read line by line
- remove new line character
- skip empty lines
- if there is a string before the semicolon but not after, use the string before twice
- if there is a string but no semicolon, use the string twice
- return a list
That's want i want to get:
[['a', 'a_desc'], ['b', 'b_desc'], ['c', 'c'], ['d', 'd'], ['e', 'e_desc']]
What i already got:
filename = 'data.txt'
with open(filename, 'r') as f:
x = [line.rstrip('\n') for line in f.readlines() if not line.isspace()]
xx = [line.split(';') for line in x]
content = [line for line in xx if line[0]]
print(content)
That will give me:
[['a', 'a_desc'], ['b', 'b_desc'], ['c', ''], ['d'], ['e', 'e_desc']]
I could probably create more loops, to catch the c and the d line right.
But is there a shorter way instead of all the loops?
Thanks!
a;;;;;
should be treated?