V
V
Vladimir Kuts2018-05-16 13:14:29
Python
Vladimir Kuts, 2018-05-16 13:14:29

Problem reading paths from file?

>>> str1 = "some\new\path\version1"
>>> myf = StringIO()
>>> myf.write(str1)
>>> myf.seek(0)
>>> print myf.getvalue()
some
ew\path
       ersion1

How to output a string in a normal form:
some\new\path\version1
I read the string str1 from another file, in which some paths are enumerated. That is, the "corrupted" string is already in the variable.
In other words, is there an analogue of this terrible crutch:
>>> print myf.getvalue().encode('string_escape').replace(r'\\', r'\\'[:-1]).replace(r'\x0b', r'\v')
some\new\path\version1

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2018-05-16
@alexr64

https://pythonworld.ru/tipy-dannyx-v-python/stroki...

A
Andy_U, 2018-05-16
@Andy_U

Use repr(str1).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question