Python - changing the output when querying my CSV file -


i have been tasked create program in python searches through csv file; list of academic papers (author, year, title, journal - it's tsv).

with current code, can achieve correct output (as in information correct), not formatted correctly.

what i'm getting is;

['albers;bergman', '1995', 'the audible web', 'proc. acm chi']

where need format;

author/s. (year). title. journal.

so commas changed full stops (periods). ; between authors should changed & sign if there 2 authors, or there should comma followed & 3 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 this. have searched python reference, google , here @ stackoverflow, couldn't come across (that understood @ least). there lot on here removing punctuation, isn't i'm after.

i first thought replace function work, gives me error. (i'll leave code in show attempting, commented out)

str.replace(',', '.') typeerror: replace() takes @ least 2 arguments (1 given) 

it wouldn't have totally solved problem, figured it's move from. i'm assume str.replace() won't take punctuation?

anyway, below code. have other ideas?

import csv   def titlesearch():     titlesearch = input("please enter title (or part of title). \n")     row in everything:         title = row[2]         if title.find(titlesearch) != -1:             print (row)   def authorsearch():     authorsearch = input("please type author name (or part of author name). \n")     row in everything:         author = row[0]         if author.find(authorsearch) != -1:           #str.replace(',', '.')         print (row)   def journalsearch():     journalsearch = input("please type in journal (or part of journal name). \n")     row in everything:         journal = row[3]         if journal.find(journalsearch) != -1:             print (row)  def yearsearch():     yearsearch = input("please type in year wish search. if wish search decade, enter first 3 numbers of decade; i.e entering '199' search papers released in 1990's.\n")     row in everything:         year = row[1]         if year.find(yearsearch) != -1:             print (row)     data = csv.reader (open('list.txt', 'rt'), delimiter='\t') = [] row in data:     everything.append(row)    while true:     searchoption = input("enter search author. \nenter j search journal name.\nenter t search title name.\nenter y search year.\nor enter other letter exit.\nif there no matches, or made mistake @ point, prompted 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 can help, it's appreciated!

what you've got far great start; need process little further. replace print(row) prettyprintcitation(row), , add function below.

basically, looks need format authors switch, best implemented function. then, can handle rest nice format string. suppose reference rows 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 following give 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 citations above, gives you

albers. (1994). audible internet. proc. acm chi. albers & bergman. (1995). 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 "@" in proposed output mistake...)


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -