9

I am opening a workbook in openpyxl thus:

wb = load_workbook(r'seven.xlsx', data_only=True)

The name of the spreadsheet won't always be know in advance, so I need to rewrite this hardcoding to allow for a variable, while still maintaining the r?

If my variable name is sheet, then:

wb = load_workbook(sheet, data_only=True)

will omit the r.

And obviously I cannot do:

wb = load_workbook(r'sheet', data_only=True)

How do we achieve the prepending of r to a variable / how do we wrap a vriable within r''?

4

2 回答 2

4

我真的不明白你想做什么,但如果你有一个字符串并且你想创建一个原始文本,我知道有两种主要方法:
raw_text = [str_text]

str_text = "%r"%str_text
raw_text = str_text[1:-1].

于 2018-08-16T12:15:18.067 回答
0

你的问题没有意义。r前缀 to 表示后面的字符串文字中的反斜杠序列将按字面解释而不是转义序列。如果您已经有一个字符串变量,则该变量的内容已经正确设置。

于 2015-07-12T06:19:34.883 回答