Python Forum
How to show newly added column to csv - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to show newly added column to csv (/thread-23567.html)



How to show newly added column to csv - johnson54937 - Jan-06-2020

I am trying to make a 'inventory' type app...
i have it working like i want it to work, adding name of item and serial number of item, but i am trying to add an item and keep getting errors...
if you can look at this and tell me how to change it to view the added item
def list_items(items):
    for i in range(0, len(items)):
        item = items[i]
        print(str(i+1) + ". " + item[0] + " (" + str(item[1]) + ")") 
Thanks in advance

I now have it where it will input the added information in the csv file, but now i can't view it... i can change the 'item[]' or even add an 'item[]' but it states out of range. i think i should simply be able to change the line 'for i in range(0, len(items)):
but can't seem to make it work...

thanks for the help


RE: How to show newly added column to csv - Larz60+ - Jan-06-2020

use difflib: https://docs.python.org/3/library/difflib.html
(or you can use Linux diff from command line)


RE: How to show newly added column to csv - hinay - Jan-06-2020

Hi, Larz60 thanks for the link to it, I was finding the same.

Regards,
H. Smith


RE: How to show newly added column to csv - Larz60+ - Jan-07-2020

forget my first post, I misunderstood your intent, is this what you're looking for?
(change csv filename to your file, and change delimiter as required)
import os
import csv


os.chdir(os.path.abspath(os.path.dirname(__file__)))

def list_items(row):
    for item in row:
        for element in item:
            print(f"{element} ", end ='')
        print(" | ", end="")
    print()

with open('health-no-head-sample.csv') as fp:
    crdr = csv.reader(fp, delimiter= ',')
    for n, row in enumerate(crdr):
        if n < 10:
            list_items(row)
        else:
            break
results with my csv file:
Output:
M E A S L E S | 2 0 6 . 9 8 | C O L O R A D O | 2 0 9 9 | 1 0 1 4 0 0 0 | 1 9 2 8 | M E A S L E S | 6 3 4 . 9 5 | C O N N E C T I C U T | 1 0 0 1 4 | 1 5 7 7 0 0 0 | 1 9 2 8 | M E A S L E S | 2 5 6 . 0 2 | D E L A W A R E | 5 9 7 | 2 3 3 0 0 0 | 1 9 2 8 | M E A S L E S | 5 3 5 . 6 3 | D I S T R I C T O F C O L U M B I A | 2 5 6 6 | 4 7 9 0 0 0 | 1 9 2 8 | M E A S L E S | 1 1 9 . 5 8 | F L O R I D A | 1 7 1 4 | 1 4 3 3 0 0 0 | 1 9 2 8 | P O L I O | 7 . 0 4 | C O L O R A D O | 7 1 | 1 0 1 4 0 0 0 | 1 9 2 8 | P O L I O | 4 . 5 3 | C O N N E C T I C U T | 7 2 | 1 5 7 7 0 0 0 | 1 9 2 8 | P O L I O | 3 . 4 4 | D E L A W A R E | 8 | 2 3 3 0 0 0 | 1 9 2 8 | P O L I O | 6 . 9 2 | D I S T R I C T O F C O L U M B I A | 3 3 | 4 7 9 0 0 0 | 1 9 2 8 | P O L I O | 1 . 4 7 | F L O R I D A | 2 1 | 1 4 3 3 0 0 0 | 1 9 2 8 |