简体   繁体   中英

Backslash to forward in unicode string in Python

I have a spreadsheet with dates, usually encoded as strings in the format "DD\\MM\\YYYY", as 08\\09\\2014. The function I use returns the data as unicode, and I use Python 2.7. So, i start with:

> data_prob_raw
08\09\2014

To convert the string to a datetime object (datetime.parser.parse()) I need a string without '\\', but I don't find a way to remove or substitute that problematic character with '/'. I already tried with unicode codes:

data_prob_raw=data_prob_raw.replace(r'\x81', '/201')
data_prob_raw=data_prob_raw.replace(u'\x81', '/201')

And simply a string:

data_prob_raw=data_prob_raw.replace('\201','/201')

But it doesn't change anything:

08\09\2014

decoding the string:

data_prob_raw=data_raw_unic.encode('ascii')

But \\201 goes uver the 128 ascii chars:

UnicodeDecodeError: 'ascii' codec can't decode byte 0x81 in position 0: ordinal not in range(128)

How can I solve that problem?

When you read data into a file from python you should get an escaped string.

I have a file called test.txt with the contents 01\\01\\2010

>>> with open(r'C:\users\john\desktop\test.txt') as f:
        s = f.read()

>>> s
'01\\01\\2010'
>>> s.replace('\\', '/')
'01/01/2010'

and I have no problem using .replace on the string. What might be happening is that you are creating a variable directly, to test the functionality, and are assigning data_prob_raw='08\\09\\2014' when you should be testing with either data_prob_raw='08\\\\09\\\\2014' or reading the date in from the file.

As zondo suggested you can also use raw stings like so; data_prob_raw=r'08\\09\\2014' . Notice the preceding r , that r tells Python to treat the backslashes as literal backslashes instead of parsing the escape characters.

To process simply a backslash in a string, you just have to put it twice. It is the escape character, so the following replace should be enough:

data_prob_raw=data_prob_raw.replace('\\', '/')

You don't need to perform replacement. datetime can parse any date format you specify:

>>> data = ur'08\09\2014'
>>> from datetime import datetime
>>> datetime.strptime(data,ur'%m\%d\%Y')
datetime.datetime(2014, 8, 9, 0, 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM