简体   繁体   中英

Extract string with bibliographic data from a .txt file into a dictionary in python

i want to code a python function, which extracts certain string elements out of a textfile with bibliographical data. The file contains different lines like that:

shakespeare, william: macbeth. novel, second edition, cambridge, 2005

Each line is been separated by a \\n .

How can i extract these lines into a structure like:

author : shakespeare, william
title : macbeth

usw.

You could do something like this. Every line would have its own dictionary then.

d = {}
s = "shakespeare, william: macbeth. novel, second edition, cambridge, 2005"
data = s.split(".")[0].split(": ")
d["author"] = data[0]
d["title"] = data[1]

print d
#Output
{'title': 'macbeth', 'author': 'shakespeare, william'}

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