[英]how to write in multiple arguments to a txt file (strings and variables)
I am trying to create a program that takes the start times and end times of word phrases, subtracts them to find the time length, and writes the length of each phrase into a file. 我正在尝试创建一个程序,该程序采用单词短语的开始时间和结束时间,减去它们以找到时间长度,然后将每个短语的长度写入文件中。 i would like the final result to be "The length of phrase #(NumTrials) is: (length)" with length and numtrials being variables.
我希望最终结果是“长度为num的短语#(NumTrials)的长度为:(length)”。 I have tried a couple different approaches, but python is telling me that it is expecting only 1 argument for the write function.
我尝试了几种不同的方法,但是python告诉我,它只希望write函数有1个参数。 how could i modify this so that each phrase has it's length documented on a new line each time?
我该如何修改它,以使每个短语的长度每次都记录在新行中?
from decimal import Decimal, getcontext
getcontext().prec = 4
def main():
myfile = open('Phrases Compiled.txt', 'w')
NumTrials = 0
SumLength = 0
StoreLengths = dict()
response = input("are there more calculations you'd like to do?")
while response != "no":
if response in ['yes', 'y', 'Yes', 'Y']:
start, stop = eval(input("what is the start and stop times of the phrase?"))
start = Decimal(start)
stop = Decimal(stop)
length = stop - start
StoreLengths[NumTrials] = length
NumTrials += 1
SumLength += length
print(length)
length = str(length)
NumTrials = str(NumTrials)
myfile.write("the length of phrase #", NumTrials, "is: ",length"\n")
response = input("are there more calculations you'd like to do?")
elif response in ['no', 'n', 'N', 'No']:
print("calculations are done")
break
averagelength = SumLength/NumTrials
print("average length was:", averagelength)
main() 主要()
You are doing concatenation wrong... 您做错了串联...
myfile.write("the length of phrase #", NumTrials, "is: ",length"\n")
Should be 应该
myfile.write("the length of phrase #" + str(NumTrials) + "is: " + str(length) + "\n")
# But even with "+" I guess it would be wrong to add strings and decimal objects together... Hence the "str(...)" but it is kinda bad.
So the best option: 所以最好的选择是:
myfile.write("the length of phrase #%d is: %f\n" % (NumTrials, length))
Edit: I guess you also misindented your code after the if response in ['yes', ...]
because Python should not allow that formating. 编辑:我想您还会
if response in ['yes', ...]
的if response in ['yes', ...]
之后错误地缩进代码if response in ['yes', ...]
因为Python不应允许这种格式。
Personally when formatting strings i like to use string.format() 就个人而言,在格式化字符串时,我喜欢使用string.format()
myfile.write("the length of phrase #", NumTrials, "is: ",length"\n")
Would become 会成为
myfile.write("the length of phrase {} is: {}\n".format(NumTrials, length))
I think you forgot to concatenate the string correctly. 我认为您忘记正确连接字符串了。 This should solve the problem:
这应该可以解决问题:
from decimal import Decimal, getcontext
getcontext().prec = 4
def main():
myfile = open('Phrases Compiled.txt', 'w')
NumTrials = 0
SumLength = 0
StoreLengths = dict()
response = input("are there more calculations you'd like to do?")
while response != "no":
if response in ['yes', 'y', 'Yes', 'Y']:
start, stop = eval(input("what is the start and stop times of the phrase?"))
start = Decimal(start)
stop = Decimal(stop)
length = stop - start
StoreLengths[NumTrials] = length
NumTrials += 1
SumLength += length
print(length)
length = str(length)
NumTrials = str(NumTrials)
myfile.write("the length of phrase #", NumTrials, "is: ", length + "\n") # Note that it's length + "\n"
response = input("are there more calculations you'd like to do?")
elif response in ['no', 'n', 'N', 'No']:
print("calculations are done")
break
averagelength = SumLength/NumTrials
print("average length was:", averagelength)
Edit: Question already answered 编辑:问题已经回答
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.