0

I'd like to error-proof user input to a file reading function. Suppose the user input string has backslashes sqlfile_in_DS="I:\DS\40-400\OppMetrics.sql" and I want the corrected string to replace those with forward slashes sqlfile_in_DS="I:/DS/40-400/OppMetrics.sql". In my attempts, the substring "/40" disappears.

Attempts and results:

1) print(pathlib.PureWindowsPath(sqlfile_in_DS))
OUTPUT:
I:/DS -400/OppMetrics.sql
    
2) print(sqlfile_in_DS.replace('\\','/'))
OUTPUT:
I:/DS -400/OppMetrics.sql

I used the answers from [Replace Backslashes with Forward Slashes in Python], Converting a string variable to a regular expression in python

If the string is a raw string literal both methods work:

sqlfile_in_DS=r"I:\DS\40-400\OppMetrics.sql"
print(pathlib.PureWindowsPath(sqlfile_in_DS))
print(sqlfile_in_DS.replace('\\', '/'))

OUTPUT:
I:/DS/40-400/OppMetrics.sql

I can't find a good explanation for converting a simple string to a raw string that works. Python regex: How to search and replace strings doesn't

print(sqlfile_in_DS.encode('unicode_escape'))
OUTPUT:
b'I:\\\\DS -400\\\\OppMetrics.sql'

print(r'{}'.format(sqlfile_in_DS))
OUTPUT:
I:\DS -400\OppMetrics.sql
DrDan
  • 29
  • 6
  • You don't EVER need to "convert" a string to a raw string. Raw strings are just a way to represent string constants within a Python script. If you read a string from a file, then it does not have any extra backslashes. It's already in the right format. – Tim Roberts Oct 23 '22 at 00:26
  • This doesn't answer the question if the string has a backslash. Clearly, converting to a raw string would solve the problem. – DrDan Oct 23 '22 at 01:28
  • There is *no such thing* as a "raw string". There are "raw string literals", but that only applies to strings written directly in your source code; the resulting string object is exactly the same kind of string as any other. – jasonharper Oct 23 '22 at 02:04
  • Solve WHAT problem? You haven't said a thing about what PROBLEM you are trying to solve. Raw strings are not the issue. The difference between a string and a raw string is the same as the difference between typing `100` and `0x64` to get an integer. Both are ways to type an integer constant in code, but if you read a 100 from a file as a 4-byte integer, clearly it is neither in decimal or hexadecimal. What end result are you trying to get? – Tim Roberts Oct 23 '22 at 07:32

0 Answers0