Python 替换不使用列表项的函数
我试图使用replace函数从列表中获取项,并用相应的值替换下面的字段,但无论我做什么,它似乎只有在到达范围的末尾时才起作用(在它的最后一个可能值I上,它成功地替换了一个子字符串,但在此之前它没有) 这就是我运行代码后得到的结果Python 替换不使用列表项的函数,python,python-2.7,replace,Python,Python 2.7,Replace,我试图使用replace函数从列表中获取项,并用相应的值替换下面的字段,但无论我做什么,它似乎只有在到达范围的末尾时才起作用(在它的最后一个可能值I上,它成功地替换了一个子字符串,但在此之前它没有) 这就是我运行代码后得到的结果 <<name>> <<color>> <<age>> <<name>> <<color>> <<age>> <<
<<name>> <<color>> <<age>>
<<name>> <<color>> <<age>>
<<name>> <<color>> 18
18
我在这件事上耽搁太久了。如有任何建议,将不胜感激。谢谢:)
完整代码:
def writeDocument():
msgFile = raw_input("Name of file you would like to create or write to?: ")
msgFile = open(msgFile, 'w+')
msg = raw_input("\nType your message here. Indicate replaceable fields by surrounding them with \'<<>>\' Do not use spaces inside your fieldnames.\n\nYou can also create your fieldname list here. Write your first fieldname surrounded by <<>> followed by the value you'd like to assign, then repeat, separating everything by one space. Example: \"<<name>> ryan <<color>> blue\"\n\n")
msg = msg.replace(' ', '\n')
msgFile.write(msg)
msgFile.close()
print "\nDocument written successfully.\n"
def fillDocument():
msgFile = raw_input("Name of file containing the message you'd like to fill?: ")
fieldFile = raw_input("Name of file containing the fieldname list?: ")
msgFile = open(msgFile, 'r+')
fieldFile = open(fieldFile, 'r')
fieldNameList = []
fieldValueList = []
fieldLine = fieldFile.readline()
while fieldLine != '':
fieldNameList.append(fieldLine)
fieldLine = fieldFile.readline()
fieldValueList.append(fieldLine)
fieldLine = fieldFile.readline()
print fieldNameList[0]
print fieldValueList[0]
print fieldNameList[1]
print fieldValueList[1]
msg = msgFile.readline()
for i in range(len(fieldNameList)):
foo = fieldNameList[i]
bar = fieldValueList[i]
msg = msg.replace(foo, bar)
print msg
msgFile.close()
fieldFile.close()
###Program Starts#####--------------------
while True==True:
objective = input("What would you like to do?\n1. Create a new document\n2. Fill in a document with fieldnames\n")
if objective == 1:
writeDocument()
elif objective == 2:
fillDocument()
else:
print "That's not a valid choice."
def writeDocument():
msgFile=原始输入(“要创建或写入的文件名:”)
msgFile=open(msgFile'w+')
msg=raw\u input(“\n请在此处键入您的消息。用\'\'包围可替换字段,以指示可替换字段。请勿在字段名中使用空格。\n\n您也可以在此处创建字段名列表。请在第一个字段名中加上您要分配的值,然后重复,将所有字段用一个空格分隔。例如:\”ryan blue \“\n\n”)
msg=msg.replace(“”,“\n”)
msgFile.write(msg)
msgFile.close()
打印“\n已成功写入文档。\n”
def fillDocument():
msgFile=raw_input(“包含要填充的消息的文件名:”)
fieldFile=原始输入(“包含fieldname列表的文件名:”)
msgFile=open(msgFile'r+')
fieldFile=打开(fieldFile,'r')
fieldNameList=[]
fieldValueList=[]
fieldLine=fieldFile.readline()
而fieldLine!='':
fieldNameList.append(字段行)
fieldLine=fieldFile.readline()
fieldValueList.append(字段行)
fieldLine=fieldFile.readline()
打印字段名称列表[0]
打印字段值列表[0]
打印字段名称列表[1]
打印字段值列表[1]
msg=msgFile.readline()
对于范围内的i(len(字段名称列表)):
foo=字段名称列表[i]
bar=字段值列表[i]
msg=msg.replace(foo,bar)
打印味精
msgFile.close()
fieldFile.close()
###节目开始#####--------------------
而True==True:
目标=输入(“您想做什么?\n1.创建新文档\n2.使用字段名填写文档\n”)
如果目标==1:
减记文件
elif目标==2:
文件(
其他:
打印“这不是一个有效的选择。”
消息文件:
<<name>> <<color>> <<age>>
字段名文件:
<<name>>
ryan
<<color>>
blue
<<age>>
18
瑞安
蓝色
18
原因:
这是因为除从“Fieldname”文件读取的最后一行外,所有行都包含“\n
”字符。因此,当程序用于替换部件时,fieldNameList
,fieldValueList
和msg
如下所示:
fieldNameList = ['<<name>>\n', '<<color>>\n', '<<age>>\n']
fieldValueList = ['ryan\n', 'blue\n', '18']
msg = '<<name>> <<color>> <<age>>\n'
这里的
msg
是什么?我们将非常感谢。我们可以通过复制粘贴来运行它,这显示了错误。@AbdulNiyasPM我添加了完整的代码和脚本使用的文件中的文本。您的完整代码不是。使用调试器可以节省您很多时间:)我如何获得调试器?使用像pycharm或spyder这样的IDE
fieldNameList = ['<<name>>\n', '<<color>>\n', '<<age>>\n']
fieldValueList = ['ryan\n', 'blue\n', '18']
msg = '<<name>> <<color>> <<age>>\n'
fieldLine = fieldFile.readline().rstrip()