Write multiple csv files in Python

Asked

Viewed 276 times

-2

Hello! My question is the following: I wrote a program that, with the information I enter, it will collect a number of links (that number varies) and will store them in a csv. Then he will open each of these links and collect some data, saving them in another csv. The problem is in this second part...

When we use something like " csvFile = open('.csv file', 'wt+') ", it will create a file called File.csv. The point is that my program will do this several times in a row, and if I keep the name the way it is it will not create an Archive(1). csv (as windows usually do with files of the same name), it will overwrite one file on top of the other. If the amount of times my program runs was predefined, I could write Think as many times as needed, the problem is that it is not, it can run it 10 times, or 1000, etc...

How can I make my program to create a csv with the collected data, and in the following link it creates a different csv, without replacing the previous file? Type 1.csv File, 2.csv File, 3.csv File, etc. This is possible?

Thanks in advance.

  • 1

    Jordan, can you make the question a little clearer? What have you tried to do? What exactly is your question? I ask why the open() opens a file for read/write, if you want to open another file just run another open() and write to another file.

  • Well, when we use something like " csvFile = open('.csv file', 'wt+') ", it will create a file called File.csv. The point is that my program will do this several times in a row, and if I keep the name the way it is it will not create an Archive(1). csv (as windows usually do with files of the same name), it will overwrite one file on top of the other. If the amount of times my program runs was predefined, I could write Think as many times as needed, the problem is that it is not, it can run it 10 times, or 1000, etc...

  • Jordan, the most effective way is for you to edit the question with these clarifications. This way it is easier to understand your question, your question goes back to the top of the most recent questions and votes to close your question temporarily can be canceled.

  • Thanks for the tip, I updated there.

  • Names should be dynamic. You can use the date and time in the file name, you can have another file that saves the last created file name, you can also read the files in the folder to find out which is the last one... there are several approaches. You just need to decide which one helps you the most. .

  • I don’t think I’ve been exactly clear yet. Suppose the program runs 5 times. It will not generate 5 csv files with the same name, but only 1, with the data collected from the last run.

  • The structure would then be, a main file with the links and a file for each link you have in that main file?

  • Fernando, that’s right!

Show 3 more comments

1 answer

0


This function below finds a unique name for the file. It keeps adding 001, 002 etc until you find a number that doesn’t exist yet.

def acha_proximo_nome(nome_arquivo):
    if not os.path.exists(nome_arquivo):
        return nome_arquivo
    base, extensao = os.path.splitext(nome_arquivo)
    numero = 1
    while True:
        nome_arquivo = '{}_{:03d}{}'.format(base, numero, extensao)
        if not os.path.exists(nome_arquivo): 
            return nome_arquivo
        numero += 1

The way to use:

f = open(acha_proximo_nome('arquivo.csv'), 'w', newline='')
  • Nosklo, sorry for the delay. Thank you very much! I did a lot of research on csv manipulation, but I couldn’t find anything to answer my question. I’ll test your response and return here, but you’re already helping too much!

  • Amigo, gave it right here!! He created the "file.csv" and the following were different files, separated by number (001, 002, 003...). That’s exactly what I wanted, thank you very much!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.