简体   繁体   中英

How do you read a text file and only print certain lines listed together?

The task is to process a text file that has all the information for an article citation. I only want to extract the author, article title, and publication; nothing else.

I want to print it in a format like this:

"Author name", "Article Title", "Publication" (AU, TI, and IP are the abbreviation that precedes every line that lists the author, title, and publication, respectively)

So far I have this code:

f = open("file.txt", "rt")
contents = f.readlines()

for line in contents:
    if "AU" in line: 
        print(line)

for line in contents:
    if "TI" in line: 
        print(line)

for line in contents:
    if "IP" in line: 
       print(line)

This code just outputs the entire list of authors, then the entire list of article titles, then the entire list of publications.

But I need it to output: Author, title, publication

How do I adjust this code to print in that format?

If they are usually consecutive lines you should do something like this:

for line in contents:
    if "IP" in line:
        print(line)
    else if "TI" in line:
        print(line)
    else if "AU" in line:
        print(line)

Note: this will print each thing in a different line. Maybe you should consider sorting everything into a dictionary that looks something like this: dict = { "authors": [], "titles": [], "publications": [] } and then print them in order like so:

for i in len(dict['authors']):
    print(dict['authors'][i] + ", " + dict['titles'][i] + ", " dict['publications'][i])

This is all considering that your data is sorted consecutively.

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