使用 pandas 读取csv文件时需要在路径名前使用'r' [英] Need of using 'r' before path-name while reading a csv file with pandas
问题描述
一个新手. 有人可以告诉我为什么在某些情况下在以下函数的路径名称前使用"r"吗?
a newbie here. Could someone tell me why do we use an 'r' in some cases before the path name in the following function?:
df = pd.read_csv(r"Path_name")
预先感谢
推荐答案
在Python中,反斜杠用于表示特殊字符.
In Python, backslash is used to signify special characters.
例如"hello \ nworld"-"\ n"表示换行符.尝试打印.
e.g. "hello\nworld" -- the "\n" means a newline. Try printing it.
Windows上的路径名称中往往带有反斜杠.但是我们希望它们表示实际的反斜杠,而不是特殊字符.
Path names on Windows tend to have backslashes in them. But we want them to mean actual backslashes, not special characters.
r代表原始",将导致字符串中的反斜杠被解释为实际的反斜杠,而不是特殊字符.
r stands for "raw" and will cause backslashes in the string to be interpreted as actual backslashes rather than special characters.
例如r"hello \ nworld"的字面意思是字符"hello \ nworld".再次尝试打印.
e.g. r"hello\nworld" literally means the characters "hello\nworld". Again, try printing it.
Python文档中提供了更多信息,建议您搜索此类问题.
More info is in the Python docs, it's a good idea to search them for questions like these.
https://docs.python.org/3/tutorial/introduction. html#strings
这篇关于使用 pandas 读取csv文件时需要在路径名前使用'r'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!