Im working on a transpiler in python right now and one function of my code is to find certain symbols and to place spaces around them so theyre easier to parse later.
This is the code that initially places to the spaces around the chars
def InsertSpaces(string):
to_return = list(string)
for i, char in enumerate(to_return):
if char == '?' or char == '#' or char == '@':
to_return[i] = (to_return[i] + ' ')[::-1]
to_return[i] += ' '
print(''.join(to_return))
Although this worked, it created an annoying problem
It created spaces right after the newlines, which could cause problems later on and is just ugly.
So this:
'@0x0D?0A\n@0x1f?@0x2f?48#65#6C#6C#6F#2C#20#57#6F#72#6C#64#21'
Becomes this:
' @ 0x0D ? 0A
@ 0x1f ? @ 0x2f ? 48 # 65 # 6C # 6C # 6F # 2C # 20 # 57 # 6F # 72 # 6C # 64 # 21'
(Keep in mind this splits up the string into a list)
So I wrote this to detect newlines inside the list in which I would remove the spaces afterwards.
for char in to_return:
char_next = to_return[to_return.index(char) + 1]
if (char + char_next) == '':
print('found a newline')
The issue is that it does not detect any newlines.
Printing the pairs of characters you can see the newline character but it cant be found by the code since it turns into a newline which is not readable by a simple string I believe.
@ 0
0x
x0
0x
D ?
? 0
0x
A
@
@ 0
0x
x0
1f
f ?
? 0
@ 0
0x
x0
2f
f ?
? 0
48
8 #
# 6
65
5 #
# 6
65
C #
# 6
65
C #
# 6
65
F #
# 6
2f
C #
# 6
2f
0x
# 6
5 #
7 #
# 6
65
F #
# 6
7 #
2f
# 6
65
C #
# 6
65
48
# 6
2f
1f
Is there a way to detect a newline character while iterating through a list of strings?