Python Forum
How to show newly added column to csv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to show newly added column to csv
#1
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
Reply
#2
use difflib: https://docs.python.org/3/library/difflib.html
(or you can use Linux diff from command line)
Reply
#3
Hi, Larz60 thanks for the link to it, I was finding the same.

Regards,
H. Smith
Reply
#4
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 |
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why does newly-formed dict only consist of last row of each year? Mark17 6 744 Nov-17-2023, 05:28 PM
Last Post: Mark17
  Python beginner that needs an expression added to existing script markham 1 665 Sep-04-2023, 05:24 AM
Last Post: Pedroski55
  PIL Image im.show() no show! Pedroski55 2 928 Sep-12-2022, 10:19 PM
Last Post: Pedroski55
  [SOLVED] [BS] Why new tag only added at the end when defined outside the loop? Winfried 1 945 Sep-05-2022, 09:36 AM
Last Post: snippsat
  PIL Image im.show() no show! Pedroski55 6 4,740 Feb-08-2022, 06:32 AM
Last Post: Pedroski55
  httplib2 - how to see credentials added by add_credentials? MSV 2 2,126 Aug-05-2020, 12:24 PM
Last Post: MSV
  Get Value from List to Show in DataFrame Column ahmedwaqas92 1 1,873 Jun-22-2020, 08:24 AM
Last Post: ahmedwaqas92
  FileNotFoundError in newly structured Python project PrateekG 0 2,402 May-23-2018, 06:20 AM
Last Post: PrateekG
  Help needed building newly released FOSS 'Meshroom' mStuff 0 2,534 Apr-29-2018, 10:54 AM
Last Post: mStuff
  Additional slashes being added to string burvil 8 13,646 Aug-04-2017, 04:15 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020