Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何记录“n”号及其对应的时间?初学者任务_Python_Python 2.7 - Fatal编程技术网

Python 如何记录“n”号及其对应的时间?初学者任务

Python 如何记录“n”号及其对应的时间?初学者任务,python,python-2.7,Python,Python 2.7,请耐心听我说,我的问题并不像看上去那么令人生畏。在第四节之前,一切都很完美 def readTemperature(): """It will read the string, extract the temperature, then return the data as a float. """ data = readTemperatureSensor.randomTemperatureGenerator() data = str(data) for line in data: if

请耐心听我说,我的问题并不像看上去那么令人生畏。在第四节之前,一切都很完美

def readTemperature():
"""It will read the string, extract the temperature, then return the data
as a float.
"""
data = readTemperatureSensor.randomTemperatureGenerator()
data = str(data)
for line in data:
    if "t" in line:
        tempBit = data.split("t")
        tempString = tempBit[1].replace("=", "")
return float(tempString)/1000
上面的代码只是读取一个温度

def getTemperatures(n):
"""Returns a list of floats that contain a certain number (n) of
temperature readings that have been sampled at an interval of one second.
"""
temperaturelist = []
for data in range(n):
    temperaturelist.append(float(readTemperature()))
    time.sleep(1)
return temperaturelist
上面的代码得到了一组相隔一秒钟测量的温度

def getTimeStamp():
    """Returns the current system date and time as a string with the format
    YYYY_MM_DD_HH_MM_SS.
    """
    return time.strftime("%Y_%m_%d_%H_%M_%S")
只是一个简单的时间戳

现在,我不知道该怎么做

def logTemperatures(n):
"""This function will record "n" temperature readings and their
corresponding time-stamps, sampled at 1 seconds intervals.
"""
基本上,它希望我创建一个文本文件,我可以这样做,但也希望我读取n个温度,并有相应的时间。如果它只是记录一组温度,那么我可以这样做,我的代码如下所示:

f = open('logTemperatures.txt', 'w')
type(f)
myList = getTemperatures(3)
currentTime = time.time()
f.write(str(time.strftime("%Y_%m_%d_%H_%M_%S",
                          time.localtime(currentTime - 0.03 * 60))))
f.write(',')
f.write(" ")
f.write(str(myList[0]))
f.write('\n')
f.write(str(time.strftime("%Y_%m_%d_%H_%M_%S",
                          time.localtime(currentTime - 0.02 * 60))))
f.write(',')
f.write(" ")
f.write(str(myList[1]))
f.write('\n')
f.write(str(time.strftime("%Y_%m_%d_%H_%M_%S",
                          time.localtime(currentTime - 0.01 * 60))))
f.write(',')
f.write(" ")
f.write(str(myList[2]))
f.close()

logTemperatures(3)
有很多f.write函数,因为我还是一个初学者,但现在我知道我可以使用+按钮来链接所有函数

文本文件如下所示:

f = open('logTemperatures.txt', 'w')
type(f)
myList = getTemperatures(3)
currentTime = time.time()
f.write(str(time.strftime("%Y_%m_%d_%H_%M_%S",
                          time.localtime(currentTime - 0.03 * 60))))
f.write(',')
f.write(" ")
f.write(str(myList[0]))
f.write('\n')
f.write(str(time.strftime("%Y_%m_%d_%H_%M_%S",
                          time.localtime(currentTime - 0.02 * 60))))
f.write(',')
f.write(" ")
f.write(str(myList[1]))
f.write('\n')
f.write(str(time.strftime("%Y_%m_%d_%H_%M_%S",
                          time.localtime(currentTime - 0.01 * 60))))
f.write(',')
f.write(" ")
f.write(str(myList[2]))
f.close()

logTemperatures(3)
时间,温度1

时间+1秒,温度2

时间+2秒,温度3

温度可能高达10摄氏度,也可能只有1摄氏度。显然,上面的代码不起作用,因为它只能在3个温度下运行,而不能在n个温度下运行。我如何修改它,使其在n个温度下都能运行?

在python 3中

n = input("Enter the number: ")
然后:


您可以编写一个函数,接收温度列表并迭代列表中的每个项目,在日志文件中为其创建一个条目

def logTemps(tempList):
    with open('logTemperatures.txt', 'w') as outfile:
        currentTime = time.time()
        for i, temp in enumerate(tempList):
            outfile.write(str(time.strftime("%Y_%m_%d_%H_%M_%S", 
                              time.localtime(currentTime - 60 * (0.01 * (len(tempList) - i))))))
            outfile.write(', ' + str(temp) + '\n')
您可以通过将上一个示例中的myList输入函数来调用该方法:

logTemps(myList)
我做过几件事:

与open'example.txt'、'w'一起使用,因为您的VariableNamehere会阻塞,而不是打开和关闭。“带文件的块”会自动关闭块末尾的文件,并向任何阅读代码的人明确指示您正在执行的操作

我做了0.01*列表的长度-当前索引,但我希望您意识到这意味着您将得到currentTime-3、currentTime-2和currentTime-1…而不是您似乎认为的currentTime、currentTime+1、currentTime+2


此外,实际上没有理由将这些写操作分隔在不同的行上,所以我只是将它们合并到一个输出文件中。像在getTemperaturesn中一样,使用for循环写入“…”

非常感谢您的帮助!我花了一点时间才理解你写的东西,对不起。尽管在枚举模板行中使用了for i,temp,但我收到一个int是不可接受的错误。我只使用过范围,所以我不明白你的脚本有什么问题。@Scarthia是圣殿骑士,你给它提供了一个温度列表?还是只有一个整数?它需要是一个列表,例如格式为[1,2,3,4]。还有,你确定你真的写了枚举吗?因为如果不是的话,它会给你这个错误。它们都包含小数点,所以没有一个是整数。我将圣殿骑士改为获取温度第二部分,因为这是我正在使用的,它确实会返回方括号内的列表,如您所示。再次感谢您的帮助!: