Writing rows and columns in csv python

Updating lines in place in a file is not supported on most file system (a line in a file is just some data that ends with newline, the next line start just after that).

As I see it you have two options:

  1. Have your data generating loops be generators, this way they won't consume a lot of memory - you'll get data for each row "just in time"
  2. Use a database (sqlite?) and update the rows there. When you're done - export to CSV

Small example for the first method:

from itertools import islice, izip, count
print list(islice(izip(count(1), count(2), count(3)), 10))

This will print

[(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10), (9, 10, 11), (10, 11, 12)]

even though count generate an infinite sequence of numbers

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Comma Separated Values (CSV) files a type of a plain text document in which tabular information is structured using a particular format.  A CSV file is a bounded text format which uses a comma to separate values. The most common method to write data from a list to CSV file is the writerow() method of writer and DictWriter class. Example 1: Creating a CSV file and writing data row-wise into it using writer class. 

    Python3

    import csv

    data = [['Geeks'], [4], ['geeks !']]

    file = open('g4g.csv', 'w+', newline ='')

    with file:   

        write = csv.writer(file)

        write.writerows(data)

    Output:

    Writing rows and columns in csv python
    Example 2: Writing data row-wise into an existing CSV file using DictWriter class. 

    Python3

    import csv

    file = open('g4g.csv', 'w', newline ='')

    with file:

        header = ['Organization', 'Established', 'CEO']

        writer = csv.DictWriter(file, fieldnames = header)

        writer.writeheader()

        writer.writerow({'Organization' : 'Google',

                         'Established': '1998',

                         'CEO': 'Sundar Pichai'})

        writer.writerow({'Organization' : 'Microsoft',

                         'Established': '1975',

                         'CEO': 'Satya Nadella'})

        writer.writerow({'Organization' : 'Nokia',

                         'Established': '1865',

                         'CEO': 'Rajeev Suri'})

    Output:

    Writing rows and columns in csv python
    Example 3: Appending data row-wise into an existing CSV file using writer class. 

    Python3

    import csv

    data = [['Geeks for Geeks', '2008', 'Sandeep Jain'],

            ['HackerRank', '2009', 'Vivek Ravisankar']]

    file = open('g4g.csv', 'a+', newline ='')

    with file:   

        write = csv.writer(file)

        write.writerows(data)

    Output:

    Writing rows and columns in csv python


    Can you write columns in CSV Python?

    Writing a CSV file by column prints the contents vertically in the output file. In the output file, a given column is written to the file before each subsequent column.

    How do you write column names in a CSV file in Python?

    Steps :.
    Open the CSV file using DictReader..
    Convert this file into a list..
    Convert the first row of the list to the dictionary..
    Call the keys() method of the dictionary and convert it into a list..
    Display the list..

    How do I add a column to a CSV file in Python?

    How to add a column to a CSV file in Python.
    df = pd. read_csv("sample.csv").
    df["new_column"] = "".
    df. to_csv("sample.csv", index=False).

    How do I write a list of elements in a CSV file in Python?

    The most common method to write data from a list to CSV file is the writerow() method of writer and DictWriter class. Example 1: Creating a CSV file and writing data row-wise into it using writer class.