我从文件中读取以下行
# subject, catagory, note
literature,general education,,note: see approved list
social-cultural elective,general education,,note: see approved
我想拆分这些以便稍后添加到字典中。所以我想分开逗号,双逗号和“:”字符。我将如何做到这一点,以便最终结果显示为
{'Literature' : [['general education'],[ 'see approved list']] 'social-cultural elective' : [['general education'],[see approved]]}
即时通讯使用重新拆分并具有“W +”表达但由于每行都有不同的空格和其他字符,例如“ - ”,帮助
答案 0 :(得分:1)
使用Python内置split函数:
>>> a_str='social-cultural elective,general education,,note: see approved '
>>> import re
>>> re.split(': |, |\*|\n', a_str)
PS:对于双逗号,您可以先执行 str.replace
答案 1 :(得分:1)
试试这个:
import re
splitter = re.compile(r":|,,?")
for line in ("literature,general education,,note: see approved list",
"social-cultural elective,general education,,note: see approved"):
print splitter.split(line)
打印:
['literature', 'general education', 'note', ' see approved list']
['social-cultural elective', 'general education', 'note', ' see approved']
如果这不完全是你想要的,请编辑你的问题,因为这正是你所要求的;-)(即“所以我想用逗号分隔,双逗号和”:“字符”)。