简体   繁体   中英

Create A New .CSV File From While Loop

I have a while loop that works really well (it does what it's supposed to). When I run the while loop and have it write it's generated dataframe to.CSV it work no problem and keeps looping (albeit overwriting the.CSV file)

But I'm trying to figure out how to write the df to a new file (with a variable generated name) each time the loop runs. I can't seem to get this figured out.

Does anyone have a suggestion?

 f = open('ActionLogLinks.csv')
    csv_f = csv.reader(f)
    action_log_links = []
    for column in csv_f:
      action_log_links.append(column[1])
    cand_ref = []
    for column in csv_f:
      cand_ref.append(column[0])
    position = 0
    while position <len(action_log_links):
        browser.get(action_log_links[position])        
        for cand in cand_ref:
            filename="Loop_Test"+"_"+str(cand_ref)+".csv"
            df.to_csv(filename, index=False)
        position = position + 1

A common solution I use is to add a timestamp to the filename when creating the output. Since you are pretty happy with the rest of the code, you will only need to update the section where you write the file, although you can put the import at the top of your script.

However, after a close read I think maybe you have an issue with how you are "looping" over the iterable. You are using cand_ref inside the loop instead of the dynamically updated during loop cand

Notice I changed your string concatenation to use the newer "f-string" format:

  from time import strftime   

  for cand in cand_ref:
        filename=f"Loop_Test_{cand}_{strftime('%Y%m%d-%H%M%S')}.csv"
        df.to_csv(filename, index=False)

You can use the following code to create and open a new file with a random name for writing.

You can also go the timestamp route, in this case make sure the precision of your timestamp is fine enough to not generate the same value in any two adjacent iterations of your loop.

import string
import random
name = "".join([random.choice(string.lowercase) for _ in range(10)]) + ".csv"
f = open(name, "w+")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM