![](/img/trans.png)
[英]Python Mysqld: After reinstall, only works if variable tuple has trailing comma
[英]python split by comma only if comma has trailing character
這是我的代碼:
with open(dict_file) as record_list:
for x in record_list:
dictionary_list.append([r for r in x[:-1].split(',')])
我僅在字符之間有逗號時才嘗試拆分,例如"abc,abc"
,但如果逗號有尾隨空格,則跳過拆分: "12, Main St"
。
使用帶有lookbehind和lookahead的正則表達式來匹配所需的:
import re
s = "abc,abc"
print(re.split(r'(?<!\s),(?!\s)', s))
# ['abc', 'abc']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.