【发布时间】:2018-11-27 00:07:05
【问题描述】:
我正在尝试在一些csv 文件中用/ 替换一些\ 字符(因为我正在从Windows 迁移到Linux,并且需要修改.csv 文件中列出的路径名。)。
我有这个:
import pandas as pd
file = 'my_file.csv'
df = pd.read_csv(file)
df = df.replace('\','/')
df.to_csv(file)
但我收到此错误:
file "<ipython-input-29-9556415d69a6>", line 5
df = df.replace('\','/')
^
SyntaxError: EOL while scanning string literal
我可以替换任何其他字符,但 \ 会导致问题,大概是因为它试图将字符串解释为路径?
我做错了什么??
【问题讨论】:
-
df.replace('\\','/')在您不想使用时转义反斜杠