字符串参数为元组 - python

时间:2017-08-22 22:31:33

标签: python tuples

我打开了一个文本文件并将其打印出来。我试图允许用户按顺序输入要在元组中打印的行的详细信息(名字或部门名称' CSEE'):

 Timothy, Johnson      12345 Law      £25000

这是我到目前为止的代码。

fob=open('c:/Users/username/Desktop/CE151 Python/ass2/info.txt','r')
print("enter student file name: )")
text_file = open("info.txt","r")
for item in fob:
    for y in item:
        item.split(' ')
        print(item)
        break
find = input('Input Details: ')
for find in item:
    print(find)

info.txt文件包含:

12345 Law 35000 Bob Liam
12346 Biology 25000 Tom Hanks
12350 Economics 30000 Billy Kid
13123 Economics 55000 Finn Balor
13124 Maths 40000 David Young
13126 Physics 25000 John Wayne Smith
13127 History 35000 Tony Gregg
13128 History 27500 Lily Joe
13129 Chemistry 25000 Saxton Crown
13130 Law 22000 jimmy Arrow

我希望用户输入信息,例如,如果用户输入历史记录,那么所有研究历史的人都将被打印

1 个答案:

答案 0 :(得分:2)

看起来您的任务非常简单 - 只需打开一个文件,然后为每一行读取一些空格分隔的数据,然后重新排列它。这可以在Python中非常简洁地完成。如果您希望用户能够以交互方式输入文件名,我不太清楚为什么您有这么多input,我认为您最多只需要一个#open the file with open("input.txt") as student_file: #for each line for line in student_file: #read some space separated data and rearrange it print("{3}, {4}\t{0}\t{1}\t£{2}".format(*line.split())) 。这是我的代码:

input.txt

Bob, Liam 12345 Law £35000 Tom, Hanks 12346 Biology £25000 Billy, Kid 12350 Economics £30000 Finn, Balor 13123 Economics £55000 David, Young 13124 Maths £40000 John, Wayne 13126 Physics £25000 Tony, Gregg 13127 History £35000 Lily, Joe 13128 History £27500 Saxton, Crown 13129 Chemistry £25000 jimmy, Arrow 13130 Law £22000 有输入的地方,这会导致输出

\t

此输出已分页,似乎与您想要的一样。如果您希望再次将其分隔,则可以用空格替换target_course = input("which course should be filtered by? ") with open("input.txt") as student_file: for line in student_file: data = line.split() if data[1] == target_course: print("{3}, {4}\t{0}\t{1}\t£{2}".format(*data)) s。如果您希望它以空格分隔但仍然对齐,则可能需要更多工作。

我认为更新代码以满足您的新要求:

which course should be filtered by? History
Tony, Gregg 13127   History £35000
Lily, Joe   13128   History £27500

实际操作示例:

namedtuple

如果想让他们按照他们喜欢的任何字段进行过滤,这可能会变得更复杂一些。我会使用from collections import namedtuple fields = ["forename", "surname", "id", "subject", "cost"] Row = namedtuple("Row", fields) target_field = input("what field do you want to filter by (one of {})? ".format(fields)) target_value = input("what value do you want to filter by? ") with open("input.txt") as student_file: for line in student_file: id_, subject, cost, forename, *surname = line.split() data = Row(id=id_, subject=subject, cost=cost, forename=forename, surname=" ".join(surname)) if getattr(data, target_field) == target_value: print("{0.surname}, {0.forename}\t{0.id}\t{0.subject}\t£{0.cost}".format(data)) ,然后检查一下。这可以完成如下:

what field do you want to filter by (one of ['forename', 'surname', 'id', 'subject', 'cost'])? subject
what value do you want to filter by? History
Gregg, Tony 13127   History £35000
Joe, Lily   13128   History £27500

what field do you want to filter by (one of ['forename', 'surname', 'id', 'subject', 'cost'])? cost
what value do you want to filter by? 25000
Hanks, Tom  12346   Biology £25000
Wayne Smith, John   13126   Physics £25000
Crown, Saxton   13129   Chemistry   £25000

随着这种重构,代码可能会变得更加清晰。请注意,我已修复了我之前实际意识到的错误 - 它无法处理多个单词姓氏。特别是作为多个姓氏的咒语及其处理非常接近我的心。它现在的工作原理如下:

cost

请注意,它还不是很复杂 - 例如,它没有进行任何输入验证,而DECLARE @tbl TABLE ( age INT, grp VARCHAR(20) ) INSERT INTO @tbl SELECT 1, 'A' UNION SELECT 12, 'A' UNION SELECT 20, 'A' UNION SELECT 19, 'B' UNION SELECT 30, 'B' UNION SELECT 11, 'B' UNION SELECT 4, 'C' UNION SELECT 14, 'C' UNION SELECT 5, 'B' UNION SELECT 16, 'D' SELECT grp AS Policy, age, under21 AS Under21, CASE WHEN under21 = 0 THEN Dense_rank() OVER( partition BY grp ORDER BY under21age DESC) ELSE 0 END AS Rank_Under_21 FROM (SELECT CASE WHEN age < 21 THEN 0 ELSE 1 END AS Under21, CASE WHEN age < 21 THEN age ELSE 0 END AS under21age, age, grp FROM @tbl) AS t 是以整数形式给出的,不以£为前缀。这是您可以尝试添加到程序中的内容。