fix this: import os import tkinter as tk from tkinter import filedialog import pandas as…
- User
fix this: import os import tkinter as tk from tkinter import filedialog import pandas as pd
GUI für Ordnerauswahl
def ordner_auswählen(): root = tk.Tk() root.withdraw() ordner = filedialog.askdirectory(title="Wähle den Ordner mit der Datei 'List.txt'") return ordner
def hauptfunktion(): ordner = ordner_auswählen() if not ordner: print("Kein Ordner ausgewählt. Abbruch.") return
dateipfad = os.path.join(ordner, "List.txt") if not os.path.exists(dateipfad): print(f"Die Datei 'List.txt' wurde im Ordner nicht gefunden: {ordner}") return zeilen_mit_trennung = [] with open(dateipfad, "r", encoding="utf-8") as f: for zeile in f: zeile = zeile.strip() if not zeile: continue if zeile == "0": vorname, nachname = "0", "0" else: teile = zeile.split(None, 1) if len(teile) == 1: vorname, nachname = teile[0], "0" else: vorname, nachname = teile[0], teile[1] zeilen_mit_trennung.append({"First Name": vorname, "Last Name": nachname}) # Speichern als CSV df = pd.DataFrame(zeilen_mit_trennung) csv_pfad = os.path.join(ordner, "List.csv") df.to_csv(csv_pfad, index=False, encoding="utf-8") print(f"CSV-Datei wurde erfolgreich gespeichert unter:\n{csv_pfad}")if name == "main": hauptfunktion()
It should create the First names in the first column and the comma indicates what the last name is. Show the last name in the 2nd column
- Assistant
- User
Traceback (most recent call last): File "D:\SK OBRT\DHZ Fitness\Mitarbeiter\David\Newsletter\name_trenner_tool_utf8.py", line 49, in hauptfunktion() File "D:\SK OBRT\DHZ Fitness\Mitarbeiter\David\Newsletter\name_trenner_tool_utf8.py", line 45, in hauptfunktion df.to_csv(csv_pfad, index=False, encoding="utf-8") File "C:\Users\1\AppData\Roaming\Python\Python310\site-packages\pandas\core\generic.py", line 3902, in to_csv return DataFrameRenderer(formatter).to_csv( File "C:\Users\1\AppData\Roaming\Python\Python310\site-packages\pandas\io\formats\format.py", line 1152, in to_csv csv_formatter.save() File "C:\Users\1\AppData\Roaming\Python\Python310\site-packages\pandas\io\formats\csvs.py", line 247, in save with get_handle( File "C:\Users\1\AppData\Roaming\Python\Python310\site-packages\pandas\io\common.py", line 863, in get_handle handle = open( PermissionError: [Errno 13] Permission denied: 'D:/SK OBRT/DHZ Fitness/Mitarbeiter/David/Newsletter\List.csv'
- Assistant
- User
use o format that shows correct german names and not weired letters.
- Assistant
To ensure that German special characters (like ä, ö, ü, ß) are correctly shown in your CSV file and not garbled ("weird letters"), you should make sure to use the correct encoding.
Recommended encoding for German characters:
"utf-8-sig"utf-8-sigadds a BOM (Byte Order Mark) at the start of the file.- This BOM helps programs like Excel on Windows correctly recognize the file's UTF-8 encoding and display special characters properly.
Update your CSV saving line from:
df.to_csv(csv_pfad, index=False, encoding="utf-8") - User
show the whole fixed script
- Assistant
Here is the complete fixed Python script with the requested comma-based splitting and saving the CSV with
utf-8-sigencoding to correctly display German special characters: - User
repeat and remove all "0" behind a ","
- Assistant
- User
you removed also "0" in the column of the first name. Leave these. Redo the task and check your previous output. Just remove the comma and the zero from the 2nd column and the thrid
- Assistant
- User
commas are still appearing after the last letter from the words in the 2nd column and the third column. make sure to delte those commas
- Assistant
- User
"," this is what I mean by comma
remove those signs
- Assistant
- User
the last name in the second and thrid columns still has "," this should not be there in the saved .csv
- Assistant
- User
still
- Assistant
- User
now its even worse. First and Last name appear in the second column. First name should be in the first column and last name in the second column
there are now these symbols: "" and this: , delete those
but keep the first and last name in first and 2nd column
- Assistant