How to 'raw text' a variable in Python?
Asked Answered
S

2

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

Stokowski answered 12/7, 2015 at 6:15 Comment(3)
Your question doesn't make any sense. The r is just for string literals.Frisian
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.Adrell
Possible related to this: https://mcmap.net/q/188774/-casting-raw-strings-python-duplicateCaril
S
5

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].

Sigismund answered 16/8, 2018 at 12:15 Comment(1)
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?Geophagy
D
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.

Dulci answered 12/7, 2015 at 6:19 Comment(7)
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''.Stokowski
What is the specific error that you have encountered? Please revise the question to provide complete background information about what you have tried and what failed.Dulci
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: #31363387Stokowski
Where in the docs does it say you have to use that? I have used openpyxl in the past (I prefer xlsxwriter now) and did not have to use raw string literals anywhere. Yes, on Windows.Sadfaced
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?Dulci
@Sadfaced The docs don't say I have to use r''. Experience on Windows shows that I do.Stokowski
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'.Dulci

© 2022 - 2024 — McMap. All rights reserved.