Automate adding new column and field names to all csv files in directories [closed]

2024/11/19 18:18:06

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.

Answer

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.
https://en.xdnf.cn/q/119925.html

Related Q&A

Connect the python app to a database using centos 7

I am new to all this I have apython app already helo.mysql.py and need to Connect the python app to a database. I am using centos 7 and have it installed on a ec2 instance if anyone can help please he…

How do I restart my program in Python? (see code)

if option == C:radius = float(raw_input("Enter Radius: ")) area = pi * radius**2print "Working..."sleep(1)print ("Area: %.2f. \n%s" % (area, hint))elif option == T:base = …

how to create a list of elements from an XML file in python

my XML <root> - <Book category="Children"><title>Harry Potter</title> <author>J.K</author> <year>2005</year> <price>29.99</price> &…

Is random.sample truly random?

I have a list with 155k files. When I random.sample(list, 100), while the results are not the same from the previous sample, they look similar. Is there a better alternative to random.sample that retur…

how to extract a table column data present in pdf and stored inside a variable python

I have 3 tables (image pasted) all 3 table(have same columns) look same and i want data of address column (yellow colour) of 3 tables stored inside a variable.

Pong Created in Python Turtle

Im new to Python but Ive coded in other languages, mainly for hardware. I made pong in Python using turtle but its a little glitchy. I was wondering if any of you could check it out and give advice. I …

How to build a Neural Network with sentence embeding concatenated to pre-trained CNN

I want to build a neural network that will take the feature map from the last layer of a CNN (VGG or resnet for example), concatenate an additional vector (for example , 1X768 bert vector) , and re-tra…

Obtaining values from columns in python

I want to obtain the top 3 cities and items based on their sales, but the only thing I can do now is return the all cities and items with their respective sales. Without using dict, can I obtain my des…

Is there a really efficient (FAST) way to read large text files in python?

I am looking to open and fetch data from a large text file in python as fast as possible (It almost has 62603143 lines - size 550MB). As I dont want to stress my computer, I am doing it by following wa…

How to extract all K*K submatrix of matrix with or without NumPy?

This is my input: row=6 col=9 6 9 s b k g s y w g f r g y e q j j a s s m s a s z s l e u s q u e h s s s g s f h s s e s g x d r h g y s s sThis is my code: r=int(input()) c=int(input()) n=min(r,c) k=…