在 Python 中保存多个文件
Saving Multiple files in Python
每次运行以下命令时,我都试图创建一个新文件。目前它创建 1 个文件并覆盖它。有没有办法让它不覆盖并为每个循环创建一个新文件?
import xml.etree.ElementTree as ET
import time
import csv
with open('OrderCSV.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
orders_data = ET.Element('orders_data')
orders = ET.SubElement(orders_data, 'orders')
##Order Details
order_reference = ET.SubElement(orders, 'order reference')
order_reference.set('',"12345")
order_date = ET.SubElement(order_reference, 'order_date')
order_priority = ET.SubElement(order_reference, 'order_priority')
order_category = ET.SubElement(order_reference, 'order_category')
delivery_service = ET.SubElement(order_reference, 'delivery_service')
delivery_service.text = row['delivery_service']
timestr = time.strftime("%Y%m%d%H%M%S")
mydata = ET.tostring(orders_data)
myfile = open(timestr, "wb")
myfile.write(mydata)
每次 运行 循环时更改变量名称,我建议使用 with 语句打开文件,因为打开文件后还必须关闭它
with open(timestr, 'wb') as myfile:
myfile.write(mydata)
编辑:我能想象到你的代码中唯一的缺陷是打开文件后没有关闭文件
你可以看看文件是否已经存在,稍等一下
while True:
timestr = time.strftime("%Y%m%d%H%M%S")
if not os.path.exists(timestr):
break
time.sleep(.1)
with open(timestr, "wb") as myfile:
mydata = ET.tostring(orders_data)
myfile.write(mydata)
您无需等待,只需增加几秒钟即可。如果您每秒处理大量文件,这将导致文件名及时向前移动。
mytime = time.time()
while True:
timestr = time.strftime("%Y%m%d%H%M%S", time.localtime(mytime))
if not os.path.exists(timestr):
break
time.sleep(.1)
with open(timestr, "wb") as myfile:
mydata = ET.tostring(orders_data)
myfile.write(mydata)
另一种选择是在循环之前获取单个时间戳并在进行时更新它。
mytime = time.strftime("%Y%m%d%H%M%S")
for index, row in enumerate(reader):
....
mytime = f"mytime-{index}"
....
每次运行以下命令时,我都试图创建一个新文件。目前它创建 1 个文件并覆盖它。有没有办法让它不覆盖并为每个循环创建一个新文件?
import xml.etree.ElementTree as ET
import time
import csv
with open('OrderCSV.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
orders_data = ET.Element('orders_data')
orders = ET.SubElement(orders_data, 'orders')
##Order Details
order_reference = ET.SubElement(orders, 'order reference')
order_reference.set('',"12345")
order_date = ET.SubElement(order_reference, 'order_date')
order_priority = ET.SubElement(order_reference, 'order_priority')
order_category = ET.SubElement(order_reference, 'order_category')
delivery_service = ET.SubElement(order_reference, 'delivery_service')
delivery_service.text = row['delivery_service']
timestr = time.strftime("%Y%m%d%H%M%S")
mydata = ET.tostring(orders_data)
myfile = open(timestr, "wb")
myfile.write(mydata)
每次 运行 循环时更改变量名称,我建议使用 with 语句打开文件,因为打开文件后还必须关闭它
with open(timestr, 'wb') as myfile:
myfile.write(mydata)
编辑:我能想象到你的代码中唯一的缺陷是打开文件后没有关闭文件
你可以看看文件是否已经存在,稍等一下
while True:
timestr = time.strftime("%Y%m%d%H%M%S")
if not os.path.exists(timestr):
break
time.sleep(.1)
with open(timestr, "wb") as myfile:
mydata = ET.tostring(orders_data)
myfile.write(mydata)
您无需等待,只需增加几秒钟即可。如果您每秒处理大量文件,这将导致文件名及时向前移动。
mytime = time.time()
while True:
timestr = time.strftime("%Y%m%d%H%M%S", time.localtime(mytime))
if not os.path.exists(timestr):
break
time.sleep(.1)
with open(timestr, "wb") as myfile:
mydata = ET.tostring(orders_data)
myfile.write(mydata)
另一种选择是在循环之前获取单个时间戳并在进行时更新它。
mytime = time.strftime("%Y%m%d%H%M%S")
for index, row in enumerate(reader):
....
mytime = f"mytime-{index}"
....