简体   繁体   English

python xlrd读取xls文件,路径前面的r是什么做的?

[英]python xlrd read xls file, what is the r in front of the path do?

I am using xlrd to read xls file in python. 我正在使用xlrd在python中读取xls文件。 The code below doesn't work: 以下代码不起作用:

import xlrd
datapath = 'C:\Users\Box Sync\pyCodes\Practice\2015_ERCOT_Hourly_Load_Data.xls'
workbook = xlrd.open_workbook(datapath)
print workbook

But after I added ar in front of the path, it worked. 但是在我在路径前添加了ar之后,它就起作用了。

import xlrd
datapath = r'C:\Users\Box Sync\pyCodes\Practice\2015_ERCOT_Hourly_Load_Data.xls'
workbook = xlrd.open_workbook(datapath)
print workbook

What does the 'r' do here? 'r'在这做什么? Thanks! 谢谢!

while passing the datapath to xlrd, you should use escape sequence for the space included in the folder name Box sync . 在将数据路径传递给xlrd时,应该使用转义序列作为文件夹名称Box sync包含的空间。 Adding an r at the starting of a string makes it a raw string , where the space is interpreted as space itself and you dont need any escape sequence. 在字符串的开头添加一个r使它成为一个原始字符串,其中空格被解释为空间本身,您不需要任何转义序列。 You can read more about raw strings on python documentation page. 您可以在python文档页面上阅读有关原始字符串的更多信息。 https://docs.python.org/2/reference/lexical_analysis.html#literals https://docs.python.org/2/reference/lexical_analysis.html#literals

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM