首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在codio中的可变长度记录上卡住

在codio中的可变长度记录上卡住
EN

Stack Overflow用户
提问于 2020-02-14 11:10:56
回答 1查看 1.1K关注 0票数 0

我的指令是:加载以竖线分隔的文件'P‘。它由每行3个字段组成:名字|姓氏|生日。

搜索名字'F‘和姓氏'L',将生日替换为'B’。以相同的竖线分隔格式写回文件。

代码语言:javascript
复制
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)

我认为我在正确的轨道上,但当我运行代码时,我得到了:

代码语言:javascript
复制
Adam|Smithers|10101960

以及数组的其余部分,而不是:

代码语言:javascript
复制
Adam|Smithers|00000000

不过,我仍然应该返回数组的其余部分。

EN

回答 1

Stack Overflow用户

发布于 2020-03-28 15:06:28

这是可行的

代码语言:javascript
复制
# Get the filepath from the command line
import sys
import re
P= sys.argv[1] 
F= sys.argv[2]
L= sys.argv[3]
B= sys.argv[4]

# ----------------------------------------------------------------
# 
# Our Helper functions:
# 
# ----------------------------------------------------------------

#
# Loads the file at filepath 
# Returns a 2d array with the data
# 
def load2dArrayFromFile(filepath):
  # Your code goes here:
  records= []
  with open(filepath, 'r') as f:
    element=f.readlines()
    for row in element:
      recordlist=row.strip('\n').split('|')
      records.append(recordlist)
  return records
#
# 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):
  # Your code goes here:
  for x in range(len(records)):
    row = records[x]
    if row[0]== firstname and row[1]==lastname:
      return x
    
  return 
        
        
# Sets the birthday of the record at the given index
# Returns: nothing
def setBirthday(records, index, newBirthday):
  # Your code goes here:
  if index== None:
    return
  records[index][2]=newBirthday

# Convert the 2d array back into a string
# Return the text of the 2d array
def makeTextFrom2dArray(records):
  # Your code goes here:
  concat=[]
  charV=""
  for row in records:
    concat.append(("|").join(row))
  charV=("\n").join(concat)
  return charV
# ----------------------------------------------------------------
# 
#  Our main code body, where we call our functions.
#  
# ----------------------------------------------------------------

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

# Your code goes here

# write the text string out to the file

outputFile = open(P,'w')
outputFile.write(output)
outputFile.close

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60219458

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档