I'm currently working on a simple txt converter. The data is loaded from a txt file into a scrolledtext widget. Now I want to read this text line by line (since I'd like to have the opportunity to edit the original data) and do the conversion based on the scrolledtext data. However, I can't get satisfying results.
If I simply print the text I got by using .get('1.0', 'end-1c')
it looks exactly like the scrolledtext data. Now I want to read it line by line with a for
loop and seperate it with the strip
and split
command. Basically the same way I was doing when reading directly from the file, but it's not working.
Input data contains a lot of spaces:
" 1267 33311225.807"
" 1268 33311224.395"
I would expect ['1267', '33311225.807']. Instead I get a new line for every digit:
[1]
[2]
[6]
[8]
...
and so on.
import tkinter.scrolledtext as tkst
from tkinter import filedialog
from tkinter import ttk
root = tk.Tk()
root.title("Converter")
root.geometry('1000x690+200+50')
tab_parent = ttk.Notebook(root)
tab1 = ttk.Frame(tab_parent)
tab_parent.add(tab1, text=" convert ")
frame1 = tk.Frame(tab1)
textPad_top = tkst.ScrolledText(master=frame1)
coordinates_text = textPad_top.get('1.0', 'end-1c')
for line in coordinates_text:
line_splitted = line.strip().split(" ")
read_coordinates = [ele for ele in line_splitted if ele.strip()]
point_id.append(read_coordinates[0])
r_value.append(read_coordinates[1])
root.mainloop()
Eventually I want to have several lists. One for every column given in the source file. I'm an absolute beginner in programming and would really appreciate your help.
When you iterate over a string, it will iterate over each character which is exactly what you're reporting.
If you want to iterate over each line, you need to split the data on newlines
for line in coordinates_text.split("\n"):
...