简体   繁体   English

反斜杠在Python中以Unicode字符串转发

[英]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. 我有一个包含日期的电子表格,通常以“ DD \\ MM \\ YYYY”的格式编码为字符串,格式为08 \\ 09 \\ 2014。 The function I use returns the data as unicode, and I use Python 2.7. 我使用的函数以Unicode形式返回数据,而我使用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 '/'. 要将字符串转换为日期时间对象(datetime.parser.parse()),我需要一个不带'\\'的字符串,但是我找不到一种用'/'删除或替换有问题的字符的方法。 I already tried with unicode codes: 我已经尝试使用unicode代码:

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: 但是\\ 201超越了128个ascii字符:

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. 当您从python将数据读取到文件中时,应该得到一个转义的字符串。

I have a file called test.txt with the contents 01\\01\\2010 我有一个名为test.txt的文件,内容为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. 而且我在字符串上使用.replace没问题。 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. 可能发生的情况是,当您应该使用data_prob_raw='08\\\\09\\\\2014'进行测试或读取时,您正在直接创建变量以测试功能并分配data_prob_raw='08\\09\\2014'文件中的日期。

As zondo suggested you can also use raw stings like so; 正如zondo所建议的那样,您也可以像这样使用原始的ing。 data_prob_raw=r'08\\09\\2014' . 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. 注意前面的rr告诉Python将反斜杠视为文字反斜杠,而不是解析转义字符。

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: datetime可以解析您指定的任何日期格式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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