使用 pandas 读取csv文件时需要在路径名前使用'r'
问题说明
一个新手. 有人可以告诉我为什么在某些情况下在以下函数的路径名称前使用"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
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcgajjk
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20