How to limit the number of lines writing CSV Python?

Asked

Viewed 47 times

0

I am new here, I am very beginner in programming and I am starting with Python. I am in a personal project that is well advanced but I am with a very specific difficulty, I want to write in a CSV file only a desired number of lines. This command below generates a giant file many times with more than 30k rows of data but by system demonstration I want to write only 50 rows of data in the file;

with open("arquivo.csv","w",encoding='UTF-8') as f:#Enter your file name.
    writer = csv.writer(f,delimiter=",",lineterminator="\n")
    writer.writerow(['username','user id','name','group'])
    for user in all_participants:
        if user.username:
            username= user.username
        else:
            username= ""
        if user.first_name:
            first_name= user.first_name
        else:
            first_name= ""
        if user.last_name:
            last_name= user.last_name
        else:
            last_name= ""
        name= (first_name + ' ' + last_name).strip()
        writer.writerow([username,user.id,name,target_group.title])

How can I limit the amount of recorded information:

They told me to use the function enumerator but I’m getting it, I’m too green still kkk

Thanks for the help!

  • 1

    I recommend reading the documentation available in the link below. It demonstrates examples of counters within loop(s) and execution flow control instructions within a loop with instruction for. https://answall.com/questions/322/qual-a-diferen%C3%A7a-entre-break-pass-e-continue-em-python

1 answer

1

The function enumerate is applied to the iterator going to the last part of a command for - and what it does is, for each item, return not only the item, but its position in the iteration - so it works like a counter.

Then just use one if in the code, and when the element is beyond the desired number, use the command break to get out of for:


limit = 50

with open("arquivo.csv","w",encoding='UTF-8') as f:#Enter your file name.
    writer = csv.writer(f,delimiter=",",lineterminator="\n")
    writer.writerow(['username','user id','name','group'])
    for counter, user in enumerate(all_participants):
        if limit and counter > limit:
            break
        if user.username:
            username= user.username
        else:
            username= ""
        if user.first_name:
            first_name= user.first_name
        else:
            first_name= ""
        if user.last_name:
            last_name= user.last_name
        else:
            last_name= ""
        name= (first_name + ' ' + last_name).strip()
        writer.writerow([username,user.id,name,target_group.title])

Here, instead of putting "50" directly into a code, I put it into a variable: this allows the same code to be used in a function, which can receive this limit as a parameter. The way I put the test if limit and counter > limit: if the variable limit is None or 0, the test always fails, and the limit is ignored.

The enumerate It works as described above. It returns a tuple of 2 elements - the (counter, item_original) - and the comma-separated variable syntax in the first part of the for works right with this, distributing the values for the respective variables (for counter, user ...);

Finally, it is legal to keep in mind that if the enumerate did not exist, or you did not know of its existence, the problem would be solved with a manually maintained counter:

...
writer.writerow(...)
counter = 0
for user in all_participants:
     counter += 1
     if counter > limit:
         break
     ...

Browser other questions tagged

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