我的任務是在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()
在此先感謝任何能夠幫助的人,真的很感謝!
非常感謝爲了您的幫助,這句話很棒! – 2013-05-03 19:21:20