2013-05-03 32 views
2

我的任務是在Python中創建一個用CSV文件搜索的程序;學術論文清單(作者,年份,標題,期刊 - 實際上是TSV)。Python - 在查詢我的CSV文件時更改輸出

使用我目前的代碼,我可以實現正確的輸出(如在信息中是正確的),但它的格式不正確。

我得到的是;

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

我需要的是這種格式;

作者/ s。 (年)。標題。日誌。

因此,逗號更改爲全站(句點)。 也是;作者之間的應該更改爲&如果有兩位作者,或者應該有一個逗號,然後是&三位或更多作者。 I.E

Glenn & Freg。 (1995年)。很酷的書名。史詩雜誌的標題。

or

佩裏史密斯@瓊斯。 (1998)。酷書書名。無聊的日記名稱。

我不完全確定如何做到這一點。我已經搜索了python引用,谷歌和這裏在Stackoverflow,但無法遇到任何東西(我的理解至少)。這裏有很多關於完全去除標點符號的問題,但這不是我所追求的。

我首先想到替換函數會起作用,但它給了我這個錯誤。 (我將離開代碼展示了我的企圖,但註釋掉)

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

它不會完全解決我的問題,但我想這件事情,從移動。我假設str.replace()不會標點符號?

無論如何,下面是我的代碼。任何人有任何其他想法?

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() 

在此先感謝任何能夠幫助的人,真的很感謝!

回答

1

你有什麼到目前爲止是一個很好的開始;你只需要進一步處理它。將print(row)替換爲PrettyPrintCitation(row),並添加下面的函數。

基本上,它看起來像你需要用交換機格式化作者,這將作爲一種功能最好地實現。然後,你可以用一個很好的格式字符串處理剩下的部分。假設你參考rows如下所示:

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'] 
] 

那麼下面就給你什麼,我相信你正在尋找:

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))) 
適用於上述引文

,這給你

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. 

(我假設你提出的輸出中的「@」是個錯誤......)

+1

非常感謝爲了您的幫助,這句話很棒! – 2013-05-03 19:21:20

0

你需要處理你的python語法。

嘗試沿着這些路線的東西:

authorlist=row[0].split(';') # split the multiple authors on semicolon 
authors=" & ".join(ahthorlist) # now join them together with ampersand 
print"""%s. (%s) %s.""" % (authorlist,row[1],row[2]) # print with pretty brackets etc.