Python Forum
Insert a multiple constant value after header in csv file using python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Insert a multiple constant value after header in csv file using python
#2
Makes a change from doing sudokus!

Maybe like this, very simple:

def myApp():
    import csv
    import glob
    # maybe use linecache?
    #import linecache

    path2csv = '/home/pedro/myPython/csv/'
    files = glob.glob(path2csv + '*.csv')
    for f in files:
        print('The csv files are', f)
        
    myfile = input('copy and paste the file you want to modify ... ')

    with open(myfile) as mf:
        mylist = mf.readlines()

    mystring = input('What do you want to put in the columns? Enter a string like AA ... ')
    # could get the number of columns automatically by splitting mylist[0] and get the len(mylist[0].split(','))
    num_cols = input('How many columns of the string do you want? ')
    num_rows = input('How many rows of columns with the string do you want? ')

    # make 1 row
    a_row = ''
    for i in range(int(num_cols)):
        if not i == int(num_cols) -1:
            a_row = a_row + mystring + ','
        else:
            a_row = a_row + mystring + '\n'

    insert_string = int(num_rows) * a_row
    headers = mylist[0]
    new_start = headers + insert_string
    # get a slice of mylist without the headers
    new_list = mylist[1:]
    new_list_string = ''.join(new_list)
    new_csv_string = new_start + new_list_string
    with open(path2csv + 'modified_csv.csv', 'w') as nf:
        nf.write(new_csv_string)

    # check the result
    with open(path2csv + 'modified_csv.csv') as infile:
        result = csv.reader(infile)
        for line in result:
            print(line)
Reply


Messages In This Thread
RE: Insert a multiple constant value after header in csv file using python - by Pedroski55 - Apr-24-2022, 10:04 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python convert multiple files to multiple lists MCL169 6 1,615 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Insert 10gb csv files into sql table via python mg24 2 1,968 Apr-28-2023, 04:14 PM
Last Post: snippsat
  Python Serial: How to read the complete line to insert to MySQL? sylar 1 850 Mar-21-2023, 10:06 PM
Last Post: deanhystad
  python pandas sql table with header mg24 3 2,009 Dec-08-2022, 08:31 PM
Last Post: Larz60+
  python insert blank line in logger mg24 1 2,894 Nov-02-2022, 08:36 AM
Last Post: snippsat
  python run multiple file paralaly mg24 2 870 Oct-11-2022, 06:08 PM
Last Post: deanhystad
  Insert header at first row, for existing csv mg24 2 2,116 Oct-05-2022, 07:12 AM
Last Post: mg24
  Add\insert header row existing csv files mg24 0 736 Oct-05-2022, 06:11 AM
Last Post: mg24
  Python create a spreadsheet with column and row header ouruslife 4 1,667 Jul-09-2022, 11:01 AM
Last Post: Pedroski55
  How to keep columns header on excel without change after export data to excel file? ahmedbarbary 0 1,184 May-03-2022, 05:46 PM
Last Post: ahmedbarbary

Forum Jump:

User Panel Messages

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