问题我想使用线程转换一个txt文件(first.txt),它的温度在C到F之间,并将输出保存到另一个文件(second.txt),这是我到目前为止遇到的问题,但我无法使它正常工作。任何帮助都将不胜感激
import os
import sys
from Queue import Queue
from threading import Thread
class ConverterWorker(Thread):
def __init__(self, queue):
Thread.__init__(self)
self.queue = queue
def run(self):
while True:
temps = self.queue.get()
self.queue.task_done()
class copy_and_convert_temps(object):
def run_it(self):
copy_and_convert_temps().do_them_together()
def do_them_together(self):
first_txt = "first.txt"
second_txt = "second_txt"
queue = Queue()
for x in range(2):
worker = ConverterWorker(queue)
worker.daemon = True
worker.start()
with open(first_txt, "r") as f1:
with open(second_txt, "w") as f2:
f_temps = f1.readlines()
for xt in f_temps:
ct = copy_and_convert_temps().convert_temps(xt)
queue.put((ct))
queue.join()
f2.writelines(ct)
def convert_temps(self, temp):
for xi in temp:
f_to_c = 9.0/5.0 * int(xi) + 32
yield (str(f_to_c) + "\n")
copy_and_convert_temps().run_it()
2
12
-3
-10
5
^should have converted C temps to F
?
?
?
?
?
?
目前没有回答
相关问题 更多 >
编程相关推荐