Hallo,
ich bin sehr neu was Python und Programmierung generell angeht, deswegen bitte simpel erklären.
Ich habe versucht eine CSV Datei zu verändern, bekomme aber immer nur einen Fehler, wie kann ich das beheben?
Code:
import Pandas as pd
with pd.read_csv('D:\P\Python Projekte\CSV korrigieren\test.csv') as file
if "\\" in file:
betterfile=file.replace("\\","/")
print(betterfile)
Fehler:
Traceback (most recent call last):
File "C:\Users\Vinstand YT\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\Vinstand YT\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "c:\Users\Vinstand YT\.vscode\extensions\ms-python.python-2021.6.944021595\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>
cli.main()
File "c:\Users\Vinstand YT\.vscode\extensions\ms-python.python-2021.6.944021595\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 444, in main
run()
File "c:\Users\Vinstand YT\.vscode\extensions\ms-python.python-2021.6.944021595\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 285, in run_file
runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
File "C:\Users\Vinstand YT\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 267, in run_path
code, fname = _get_code_from_file(run_name, path_name)
File "C:\Users\Vinstand YT\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 242, in _get_code_from_file
code = compile(f.read(), fname, 'exec')
File "d:\P\Python Projekte\CSV korrigieren\Project CSV corrector.py", line 3
with pd.read_csv('D:\P\Python Projekte\CSV korrigieren\test.csv') as file
^
SyntaxError: invalid syntax
CSV Dateien bearbeiten
@Vinstand,
du hast da einiges vermischt.
Wenn du eine csv-Datei in einen pandas DataFrame laden willst, geht das so:
Wenn du das csv Modul benutzen möchtest, geht das so:
du hast da einiges vermischt.
Wenn du eine csv-Datei in einen pandas DataFrame laden willst, geht das so:
Code: Alles auswählen
import pandas as pd
df = pd.read_csv('D:\P\Python Projekte\CSV korrigieren\test.csv')
Code: Alles auswählen
import csv
with open('D:\P\Python Projekte\CSV korrigieren\test.csv') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
print(row)