Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.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中保存多个文件_Python_Csv_Read Data - Fatal编程技术网

在Python中保存多个文件

在Python中保存多个文件,python,csv,read-data,Python,Csv,Read Data,每次运行以下命令时,我都尝试创建一个新文件。此时,它创建了一个文件并将其覆盖。是否有一种方法使其不被覆盖,并为每个循环创建一个新文件 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

每次运行以下命令时,我都尝试创建一个新文件。此时,它创建了一个文件并将其覆盖。是否有一种方法使其不被覆盖,并为每个循环创建一个新文件

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)

编辑:我能想象到的代码中唯一的缺陷是在打开文件后不关闭它

每次运行循环时更改变量名,我建议使用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}"
     ....

您可以查看文件是否已经存在,然后稍等

    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}"
     ....

我修正了一些缩进错误。。。我做对了吗?文件是否写入for循环?每个文件的运行速度是否超过1秒?仍然为零。只创建了一个文件问题是缩进是否正确(文件写入在for循环中)以及处理速度有多快。我认为您每秒生成多个,因此计算的时间戳保持不变。我还假设您要处理多个文件。@Mayamiko您需要获得唯一的文件名。我修复了一些缩进错误。。。我做对了吗?文件是否写入for循环?每个文件的运行速度是否超过1秒?仍然为零。只创建了一个文件问题是缩进是否正确(文件写入在for循环中)以及处理速度有多快。我认为您每秒生成多个,因此计算的时间戳保持不变。我还假设您要处理的文件不止一个。@Mayamiko您需要获得唯一的文件名。我相信timestr每次都会创建一个唯一的文件名,但它只是在覆盖旧的文件名one@Mayamiko尝试不同的命名(约定?)比如str(row)@Mayamiko您尝试过使用with语句吗?i fthat not work使用一些简单的东西,比如c=0,filename是str(c),并将cI相加。我相信timestr每次都会创建一个唯一的文件名,但它只是覆盖了旧的文件名one@Mayamiko尝试不同的命名(约定?)比如str(row)@Mayamiko您尝试过使用with语句吗?我用一些简单的东西,比如c=0,文件名是str(c),把cOkay加起来,为什么结果会不同呢?!我想是飞行员失误。好吧,为什么这会是一个不同的答案?!我想是飞行员失误了。