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

Pyderman
  • 14,809
  • 13
  • 61
  • 106
  • 2
    Your question doesn't make any sense. The `r` is just for *string literals*. – jonrsharpe Jul 12 '15 at 06: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. – Thomas Kimber Oct 24 '18 at 16:50
  • Possible related to this: https://stackoverflow.com/questions/2428117/casting-raw-strings-python – 林果皞 Jun 24 '20 at 15:22

2 Answers2

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

Doren
  • 139
  • 1
  • 6
  • 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? – user8491363 Feb 17 '20 at 15:09
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.

200_success
  • 7,286
  • 1
  • 43
  • 74
  • 1
    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''`. – Pyderman Jul 12 '15 at 06:27
  • 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. – 200_success Jul 12 '15 at 06:33
  • 1
    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: http://stackoverflow.com/questions/31362887/even-with-r-prepended-to-filename-openpyxl-not-able-to-save-on-windows – Pyderman Jul 12 '15 at 06:36
  • Where in the [docs](http://openpyxl.readthedocs.org/en/latest/tutorial.html) 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. – SiHa Jul 12 '15 at 06:39
  • 3
    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? – 200_success Jul 12 '15 at 06:46
  • 2
    @SiHa The docs don't say I have to use `r''`. Experience on Windows shows that I do. – Pyderman Jul 12 '15 at 07:18
  • 1
    [Experience is no substitute for reasoning.](https://en.wikipedia.org/wiki/Cargo_cult_programming) 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'`. – 200_success Jul 12 '15 at 07:22