Python - changing the output when querying my CSV file

I have been tasked to create a program in Python which searches through a CSV file; a list of academic papers (Author, Year, Title, Journal - it's actually TSV).

With my current code, I can achieve correct output (as in the information is correct), but it is not formatted correctly.

What I'm getting is;

['Albers;Bergman', '1995', 'The audible Web', 'Proc. ACM CHI']

Where as what I need is this format;

Author/s. (Year). Title. Journal.

So the commas are changed for full stops (periods). Also the ; between authors should be changed for an & sign if there are two authors, or there should be a comma followed by an & for three or more authors. I.E

Glenn & Freg. (1995). Cool book title. Epic journal title.


Perry, Smith @ Jones. (1998). Cooler book title. Boring journal name.

I'm not entirely sure how to do this. I have searched the python reference, google and here at Stackoverflow, but couldn't come across anything (that I understood at least). There is a LOT on here about completely removing punctuation, but that isn't what I'm after.

I first thought the replace function would work, but it gives me this error. (I'll leave the code in to show what I was attempting, but commented out)

str.replace(',', '.')
TypeError: replace() takes at least 2 arguments (1 given)

It wouldn't have totally solved my problem, but I figured it's something to move from. I'm assume str.replace() won't take punctuation?

Anyway, below is my code. Anybody have any other ideas?

import csv

def TitleSearch():
    titleSearch = input("Please enter the Title (or part of the title). \n")
    for row in everything:
        title = row[2]
        if title.find(titleSearch) != -1:
            print (row)

def AuthorSearch():
    authorSearch = input("Please type Author name (or part of the author name). \n")
    for row in everything:
        author = row[0]
        if author.find(authorSearch) != -1:
          #str.replace(',', '.')
        print (row)

def JournalSearch():
    journalSearch = input("Please type in a Journal (or part of the journal name). \n")
    for row in everything:
        journal = row[3]
        if journal.find(journalSearch) != -1:
            print (row)

def YearSearch():
    yearSearch = input("Please type in the Year you wish to search. If you wish to search a decade, simply enter the first three numbers of the decade; i.e entering '199' will search for papers released in the 1990's.\n")
    for row in everything:
        year = row[1]
        if year.find(yearSearch) != -1:
            print (row)

data = csv.reader (open('List.txt', 'rt'), delimiter='\t')
everything = []
for row in data:

while True:
    searchOption = input("Enter A to search by Author. \nEnter J to search by Journal name.\nEnter T to search by Title name.\nEnter Y to search by Year.\nOr enter any other letter to exit.\nIf there are no matches, or you made a mistake at any point, you will simply be prompted to search again. \n" )

    if searchOption == 'A' or searchOption =='a':

    elif searchOption == 'J' or searchOption =='j':

    elif searchOption == 'T' or searchOption =='t':
    elif searchOption == 'Y' or searchOption =='y':

Thanks in advance to anybody who can help, it's really appreciated!


What you've got so far is a great start; you just need to process it a little further. Replace print(row) with PrettyPrintCitation(row), and add the function below.

Basically, it looks like you need to format the authors with a switch, which would best be implemented as a function. Then, you can handle the rest with just a nice format string. Suppose your reference rows look like the following:

references = [
    ['Albers', '1994', 'The audible Internet', 'Proc. ACM CHI'],
    ['Albers;Bergman', '1995', 'The audible Web', 'Proc. ACM CHI'],
    ['Glenn;Freg', '1995', 'Cool book title', 'Epic journal title'],
    ['Perry;Smith;Jones', '1998', 'Cooler book title', 'Boring journal name']

Then the following will give you what I believe you're looking for:

def PrettyPrintCitation(row) :
    def adjustauthors(s):
        authorlist = s[0].split(';')
        if(len(authorlist)<2) :
            s[0] = authorlist[0]
        elif(len(authorlist)==2) :
            s[0] = '{0} & {1}'.format(*authorlist)
        else :
            s[0] = ', '.join(authorlist[:-1]) + ', & ' + authorlist[-1]
        return s

    print('{0}. ({1}). {2}. {3}.'.format(*adjustauthors(row)))

applied to the citations above, this gives you

Albers. (1994). The audible Internet. Proc. ACM CHI.
Albers & Bergman. (1995). The audible Web. Proc. ACM CHI.
Glenn & Freg. (1995). Cool book title. Epic journal title.
Perry, Smith, & Jones. (1998). Cooler book title. Boring journal name.

(I'm assuming that "@" in your proposed output was a mistake...)

Need Your Help

How to tag Users to Company?

python xml openerp

As Administrator I created two companies called MLC &amp; BLC.

reading from array but unable to find a match

php arrays

I have an array $result which looks like this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.