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''?

3
  • 2
    Your question doesn't make any sense. The r is just for string literals. Commented Jul 12, 2015 at 6:34
  • 4
    The question makes perfect sense - there are times when you want to load configuration into a process from a text file, and in some instances, that text is liable to contain "tricky" characters. For example, say you want to parse a collection of regexes from json, or an old-style .ini file for some parameterised search. At some point, you've got to decode the sanitized string to turn it back into a "hot" one that can be parsed by the regex compiler. Commented Oct 24, 2018 at 16:50
  • Possible related to this: stackoverflow.com/questions/2428117/casting-raw-strings-python Commented Jun 24, 2020 at 15:22

2 Answers 2

6

I did not understand really what you were trying to do, but if you have a string and you want to create a raw text there are two main methods I know of:
raw_text = [str_text]
and
str_text = "%r"%str_text
raw_text = str_text[1:-1].

Sign up to request clarification or add additional context in comments.

1 Comment

This worked and just saved my night. But could you explain how it works? I've never seen a string operation like that. I'm on Python 3.7 so maybe it's from the older Python?
0

Your question makes no sense. The r prefix to indicates that backslash-something sequences in the string literal that follows is to be interpreted literally rather than as escape sequences. If you already have a string variable, then the contents of the variable are already correctly set.

7 Comments

I invite you to then to try openpyxl.save() on a Windows machine for a file name (such as in the example above) that does not contain backslashes, and omitting r''.
I don't think the example in question adds any context (it's openpyxl.loadworkbook(), but really it could be anything where a raw string is required but I want to pass a variable. Here it is anyway: stackoverflow.com/questions/31362887/…
You must have misdiagnosed the problem. r'all_done.xlsx' is exactly the same as 'all_done.xlsx', since there are no backslashes inside the string literal. If it's unable to write the file, then the cause must be something else. If I had to speculate, maybe the "invalid mode" part is relevant — perhaps you don't have write permission to the current directory?
@SiHa The docs don't say I have to use r''. Experience on Windows shows that I do.
Experience is no substitute for reasoning. The r prefix just makes it so that you can write r'C:\some\path\to\an.xlsx', which is less cumbersome than the equivalent 'C:\\some\\path\\to\\an.xlsx'.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.