我想分割我的文本并将其存储以做出有序的字典
For example:
1.This is my text.
2.This is 2nd Text.
我想分割数字和文本并将其存储在有序的字典中,例如
Ordered Dict
"1":"This is my text"
"2":"This is 2nd text"
我尝试过。分裂,但对我不起作用。该怎么做?
d = OrderedDict()
text_data = [ "1.This is my text.","2.This is 2nd text"]
for i, f in enumerate(text_data):
id = f.split('.')
d[id] = text_data[i]
print(i, " :: ", id, " =>\n", d[id], "\n" + "*" * 100 + "\n")
我要去哪里错了?制作OrderedDict
答案 0 :(得分:1)
您非常亲密。按点分割字符串后,使用索引访问元素。
例如:
from collections import OrderedDict
d = OrderedDict()
text_data = [ "1.This is my text.","2.This is 2nd text"]
for i, f in enumerate(text_data):
val = f.split('.') #str.split
d[val[0]] = val[1] #Use Index.
for k, v in d.items():
print(k, v)
答案 1 :(得分:0)
我建议以下内容:
from collections import OrderedDict
d = OrderedDict()
text_data = [ "1.This is my text.", "2.This is 2nd text"]
for sentence in text_data: # Note 1
num, text = sentence.rstrip('.').split('.', 1) # Notes 2 & 3
d[num] = text
注意:
i
中的enumerate
,因此将其删除。rstrip
,然后split
之前。如您所见,每个句子的末尾都有一个点('.'
),可能会干扰split
。但是,如果要保留最后一个点(如果存在),只需删除.rstrip('.')
部分。split
传递第二个参数,告诉它应该执行多少个 cut 。想想'3. A sentence. With a dot in between.'
的情况。以上内容产生:
for k, v in d.items():
print('{!r}: {!r}'.format(k, v))
# '1': 'This is my text'
# '2': 'This is 2nd text'
答案 2 :(得分:0)
或者也许:
from collections import OrderedDict
news='.'.join(s.split('. ')).split('.')
d=OrderedDict(list(dict(news[i:i+2] for i in range(len(news)-2)).items())[::2])
for k,v in d.items():
print('"%s": "%s"'%(k,v))
输出:
"1": "This is my text"
"2": "This is 2nd Text"