-2

This question does not link with any question and does not have any answer to the problem

I need help to add new column in index (0) and field names to all csv files in directories and sub-directories. I have csv files like this

apple   chocolate    smoothies
orange  dark cocoa   ice-coffee
grapes  brownies     hot-choco
banana  oreo         ice-cream

I want my output csv files to be like this

Key         A       C            S
Record_01   apple   chocolate    smoothies
Record_02   orange  dark cocoa   ice-coffee
Record_03   grapes  brownies     hot-choco
Record_04   banana  oreo         ice-cream

Firstly, I am trying to write the same Record column to all csv files in folder and nested folders.
Secondly, I am trying to write the same Field name to all csvfiles in folder and nested folders

I have tried this code

key = []
for row, Record in enumerate(key):
    print('Record_{}'.format(row, Record))
    row.append(row[0])
    key.appen(row)

for roots, dirs, files in os.walk('path'):
    for csvfiles in files:
        if os.path.splitext(csvfiles)[0] == '.csv':
            with open(f'{csvfiles}', 'r') as csvinput:
                with open(f'{csvfiles}', 'w') as csvoutput:
                    writer = csv.writer(csvoutput, delimiter=',')
                    writer.writerow[key]

I have basically no idea how to add Record_{} by automatically to all csv files. please help me. Thank you for any answers.

Wai
  • 63
  • 6
  • Do you know the headers you want to add in advance? Are they always the same? – Tarik Jul 16 '20 at 07:46
  • How big are your files? – Tarik Jul 16 '20 at 07:46
  • 1
    There are so many questions and issues with your code. Work on one problem at a time. See how to create a [mcve]. – Peter Wood Jul 16 '20 at 07:47
  • @Tarik The files are not really just couple of MB. The headers are the same to all file. Thankyou – Wai Jul 16 '20 at 07:49
  • This question already has answers here: [how-to-add-a-new-column-to-the-beginning-of-csv-file](https://stackoverflow.com/questions/4872077) – stovfl Jul 16 '20 at 08:00
  • @stovfl actually no. I have read that post several time. It has to know the column name to write that code. I have no field name. Please ready the above post. I appreciate if you can really help. – Wai Jul 16 '20 at 08:04
  • ***I have no field name.***: Your output shows: **`Key`**? – stovfl Jul 16 '20 at 08:08
  • that's what i want my output. – Wai Jul 16 '20 at 08:09
  • @stovfl The csv file I have has no fieldname and I want to add Record column and field name automatically. – Wai Jul 16 '20 at 08:10
  • ***The csv file I have has no fieldname***: Add it, using `.DictWriter(..., fieldnames)`. Read up on [`csv.DictWriter`](https://docs.python.org/3/library/csv.html?highlight=csv%20dictwriter#csv.DictWriter) and [`DictWriter.writeheader()`](https://docs.python.org/3/library/csv.html?highlight=csv%20dictwriter#csv.DictWriter.writeheader) – stovfl Jul 16 '20 at 08:20
  • @stovfl I cannot add new fieldname to becuase I have to append new column called Record and after that I could probably use your comment – Wai Jul 16 '20 at 08:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/217949/discussion-between-stovfl-and-wai). – stovfl Jul 16 '20 at 08:27

1 Answers1

2

You have several options:

  • Read the CSV data into a Pandas DataFrame specifying no columns. Dummy columns names will be generated. Rename the columns and save the DataFrame back to CSV.
  • Open a new file, write the header, read the original file and write it to the new file. Close both files. Delete the original file and rename the new file to the old file name.
Tarik
  • 10,810
  • 2
  • 26
  • 40