Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created September 30, 2023 01:47

Revisions

  1. code-boxx created this gist Sep 30, 2023.
    23 changes: 23 additions & 0 deletions 0-PY-CSV-ADD.MD
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    ## PYTHON CSV ADD NEW ROWS
    https://code-boxx.com/python-add-rows-csv/

    ## LICENSE
    Copyright by Code Boxx

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
    8 changes: 8 additions & 0 deletions 1-append.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    # (A) LOAD CSV MODULE
    import csv

    # (B) OPEN CSV & APPEND ROWS
    with open("demo.csv", "a", newline="") as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(["A", "B"])
    writer.writerows([["C", "D"], ["E", "F"]])
    14 changes: 14 additions & 0 deletions 2-prepend.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    # (A) LOAD CSV MODULE
    import csv

    # (B) READ EXISTING ROWS INTO A LIST
    with open("demo.csv", "r", newline="") as csvfile:
    rows = list(csv.reader(csvfile))

    # (C) PREPEND NEW ROWS
    rows = [["G", "H"], ["I", "J"]] + rows

    # (D) SAVE UPDATED CSV
    with open("demo.csv", "w", newline="") as csvfile:
    writer = csv.writer(csvfile)
    writer.writerows(rows)
    14 changes: 14 additions & 0 deletions 3-insert.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    # (A) LOAD CSV MODULE
    import csv

    # (B) READ EXISTING ROWS INTO A LIST
    with open("demo.csv", "r", newline="") as csvfile:
    rows = list(csv.reader(csvfile))

    # (C) INSERT ROWS
    rows.insert(3, ["K", "L"])

    # (D) SAVE UPDATED CSV
    with open("demo.csv", "w", newline="") as csvfile:
    writer = csv.writer(csvfile)
    writer.writerows(rows)
    21 changes: 21 additions & 0 deletions 4-search.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    # (A) LOAD CSV MODULE
    import csv

    # (B) READ EXISTING ROWS INTO A LIST
    with open("demo.csv", "r", newline="") as csvfile:
    rows = list(csv.reader(csvfile))

    # (C) INSERT BEFORE "JON DOE"
    at = 0
    for i, r in enumerate(rows):
    if "Jon Doe" in r:
    at = i
    break

    # (D) INSERT NEW ROWS
    rows.insert(at, ["M", "N"])

    # (E) SAVE UPDATED CSV
    with open("demo.csv", "w", newline="") as csvfile:
    writer = csv.writer(csvfile)
    writer.writerows(rows)
    5 changes: 5 additions & 0 deletions demo.csv
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    Job Doe,[email protected]
    Joe Doe,[email protected]
    Joi Doe,[email protected]
    Jon Doe,[email protected]
    Joy Doe,[email protected]