简体   繁体   中英

Why python3.7 returns different output than python3.6(anaconda) with re.split()?

#test.py
import re

tmp = {'value': 'V-4056366 ', 'bool': False}
def get_split():
    value = str(tmp['value']).strip()
    if value=='':
        return None
    if value.isalnum(): 
        return None
    return re.split(r'[-.]*',value)

print(get_split())

If I run the above code with the pre-installed python 3.7 I get the following output (the output that I really want to get).

[pc@pc-pc PasivicSoftware]$ python3.7 test.py
['', 'V', '', '4', '0', '5', '6', '3', '6', '6', '']
[pc@pc-pc PasivicSoftware]$ which python3.7
/bin/python3.7

Now with anaconda 3.6.5:

[pc@pc-pc PasivicSoftware]$ python3 test.py
/home/pc/anaconda3/lib/python3.6/re.py:212: FutureWarning: split() requires a non-empty pattern match.
  return _compile(pattern, flags).split(string, maxsplit)
['V', '4056366'][pc@pc-pc PasivicSoftware]
$ which python3
/home/pc/anaconda3/bin/python3

Did python3.7 change the behavior of re.split? If so, how can I get the same output of anaconda (just two elements in a list)

In 3.6 you get the warning that this is going to happen:

FutureWarning: split() requires a non-empty pattern match.

Since [-]* can match zero-length string, it does that in 3.7. You can prevent that by using something that doesn't. Either - or -+ will do (depending on what you want to happen for V--12345 .

If you don't care about the consecutive - signs, you can even do value.split('-') instead, without requiring re .

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