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.
or
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:
everything.append(row)
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':
AuthorSearch()
print('\n')
elif searchOption == 'J' or searchOption =='j':
JournalSearch()
print('\n')
elif searchOption == 'T' or searchOption =='t':
TitleSearch()
print('\n')
elif searchOption == 'Y' or searchOption =='y':
YearSearch()
print('\n')
else:
exit()
Thanks in advance to anybody who can help, it's really appreciated!