Codierungsfehler unter Windows behoben und Carriage Return Symbole entfernt.

Signed-off-by: mputzlocher <m.putzlocher@stiftland-gymnasium.de>
This commit is contained in:
Martin Putzlocher 2022-01-18 15:47:02 +00:00
parent 5e014d62e9
commit 5f7342f42b

View File

@ -1,4 +1,6 @@
import re import re
import sys
import io
import tkinter as tk import tkinter as tk
import tkinter.filedialog as tkfd import tkinter.filedialog as tkfd
import tkinter.messagebox as tkm import tkinter.messagebox as tkm
@ -9,26 +11,37 @@ root.rowconfigure(0, weight=3)
root.columnconfigure(0, weight=1) root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=1) root.columnconfigure(1, weight=1)
root.columnconfigure(2, weight=1) root.columnconfigure(2, weight=1)
root.columnconfigure(3, weight=1)
content = "" content = ""
content_lines = list() content_lines = list()
newcontent = "" newcontent = ""
fname = "" fname = ""
if sys.platform == "win32":
out_encoding = "cp1252"
else:
out_encoding = "utf-8"
def load_file(event=None): def load_file(event=None):
global content global content
global content_lines global content_lines
global fname global fname
fname = tkfd.askopenfilename(filetypes=(("CSV files","*.csv"),("text files","*.txt"))) fname = tkfd.askopenfilename(filetypes=(("CSV files","*.csv"),("text files","*.txt")))
try: try:
f = open(fname, mode='r') f = io.open(fname, mode='rt', encoding='utf-8', newline="") # mit allen Steuerzeichen
content = f.read() content = f.read()
f.seek(0) content = content.replace("\r","")
for line in f: content_orig_lines = content.split("\n")
for line in content_orig_lines:
line = line.replace("\r","")
print(line)
content_lines.append(line) content_lines.append(line)
t_preview.insert(0.0,content) t_preview.insert(0.0,content)
f.close() f.close()
except: except Exception as e:
print(e)
content = [""]
tkm.showerror(title="Fehler beim Öffnen", tkm.showerror(title="Fehler beim Öffnen",
message="Öffnen der Datei fehlgeschlagen.") message="Öffnen der Datei fehlgeschlagen.")
@ -44,8 +57,12 @@ def clean(event=None):
tc = [e.strip() for e in tc] tc = [e.strip() for e in tc]
print(tc) print(tc)
# Umsortieren # Umsortieren
tcnew = [tc[-1]] + [tc[1]] + [tc[0]] + tc[2:-1] try:
tcnew[-1] = tcnew[-1] + "\n" tcnew = [tc[-1]] + [tc[1]] + [tc[0]] + tc[2:-1]
tcnew[-1] = tcnew[-1] + "\n"
except Exception as e:
print(e)
break
newline = ",".join(tcnew) newline = ",".join(tcnew)
newcontent += newline newcontent += newline
t_preview.insert(0.0,newcontent) t_preview.insert(0.0,newcontent)
@ -54,6 +71,7 @@ def clean(event=None):
def save_file(event=None): def save_file(event=None):
global fname global fname
global newcontent global newcontent
global out_encoding
if fname == "": if fname == "":
tkm.showerror(title="Fehler beim Speichern", tkm.showerror(title="Fehler beim Speichern",
@ -62,11 +80,12 @@ def save_file(event=None):
fname_new = fname[:-4] + "c" + ".csv" fname_new = fname[:-4] + "c" + ".csv"
try: try:
f = open(fname_new, mode='w') f = open(fname_new, mode='w', encoding=out_encoding, errors='replace')
f.write(newcontent) f.write(newcontent)
tkm.showinfo(title="Gespeichert", tkm.showinfo(title="Gespeichert",
message="Datei unter {} gespeichert.".format(fname_new)) message="Datei unter {} gespeichert.".format(fname_new))
except: except Exception as e:
print(e)
tkm.showerror(title="Fehler beim Speichern", tkm.showerror(title="Fehler beim Speichern",
message="Speichern fehlgeschlagen") message="Speichern fehlgeschlagen")
@ -86,7 +105,7 @@ bt_quit = tk.Button(root, text="Beenden", fg="red")
bt_quit.grid(row=0, column=3) bt_quit.grid(row=0, column=3)
bt_quit["command"] = root.destroy bt_quit["command"] = root.destroy
t_preview = tk.Text(root, height = 10, width = 150) t_preview = tk.Text(root, height = 15, width = 150)
t_preview.grid(row=1, column=0, columnspan=3, sticky="nwse") t_preview.grid(row=1, column=0, columnspan=3, sticky="nwse")
root.mainloop() root.mainloop()