我的任务是用Python创建一个程序,该程序可搜索CSV文件。一系列学术论文(作者,年份,职称,期刊-实际上是TSV)。
使用当前的代码,我可以实现正确的输出(因为信息正确),但是格式不正确。
我得到的是;
[“ Albers; Bergman”,“ 1995”,“ The audible Web”,“ Proc。 ACM CHI']
我需要的格式是哪里?
作者。 (年)。标题。日志。
因此逗号更改为句号(句号)。
也是;如果有两个作者,则应在作者之间更改&符号,或者对于三个或三个以上作者,应在逗号后跟一个&号。
。
Glenn&Freg。 (1995)。很酷的书名。史诗般的期刊标题。
要么
佩里·史密斯@琼斯。 (1998)。较酷的书名。无聊的期刊名称。
我不太确定该怎么做。我在Stackoverflow上搜索了python参考,google和此处,但是什么也没发现(至少我了解)。关于完全删除标点符号还有很多,但这不是我要的。
我最初以为replace函数会起作用,但是它给了我这个错误。 (我将保留代码以显示我的尝试,但将其注释掉)
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()
在此先感谢任何可以提供帮助的人,我们非常感谢!
最佳答案
到目前为止,您已经有了一个很好的开始。您只需要进一步处理即可。将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.
(我假设您建议的输出中的“ @”是错误的……)
关于python - Python-查询CSV文件时更改输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16350323/