我的指示是:加载管道分隔的文件“P”。它由每行 3 个字段组成:名字|姓氏|生日。
搜索名字“F”和姓氏“L”,将生日替换为“B”。以相同的管道分隔格式写回文件。
import sys
import re
P= sys.argv[1]
F= sys.argv[2]
L= sys.argv[3]
B= sys.argv[4]
roster = []
# Loads the file at filepath
# Returns a 2d array with the data
#
def load2dArrayFromFile(filepath):
file1 = open(filepath, 'r')
data = file1.read()
file1.close()
for x in range(0, len(roster)):
roster.append(data)
# Searches the 2d array 'records' for firstname, lastname.
# Returns the index of the record or -1 if no record exists
#
def findIndex(records, firstname, lastname):
for x in roster:
if re.search(firstname, roster):
if re.search(lastname, roster):
name = roster[x]
# Sets the birthday of the record at the given index
# Returns: nothing
def setBirthday(records, index, newBirthday):
for x in roster:
if re.match(name, roster[x]):
roster[x][2] = newBirthday
# Convert the 2d array back into a string
# Return the text of the 2d array
def makeTextFrom2dArray(records):
(', ').join(str(roster))
# Load our records from the file into a 2d array
records= load2dArrayFromFile(P)
# Find out which index, if any, has the name we are hunting
indexWeAreHunting= findIndex(records, F, L)
# Set the birthday record to the one we were passed
setBirthday(records, indexWeAreHunting, B)
# Convert the records into a text string
output= makeTextFrom2dArray(records)
我认为我走在正确的轨道上,但是当我运行代码时,我回来了:
Adam|Smithers|10101960
连同数组的其余部分,而不是:
Adam|Smithers|00000000
虽然,我仍然应该返回数组的其余部分。