Python Forum
Add\insert header row existing csv files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add\insert header row existing csv files
#1
Hi Team,

I am opening existing csv files, and Want to insert records at first row.

if I use inplace=True , file get overwrites
if I use inplace=False , nothing get changed.

import fileinput

for line in fileinput.input(files=["my-file.csv"], inplace=True):
    if fileinput.isfirstline():
        print('first,second,third,fourth')
    print(line, end="")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open files in an existing window instead of new Kostov 2 324 Apr-13-2024, 07:22 AM
Last Post: Kostov
  Insert 10gb csv files into sql table via python mg24 2 1,946 Apr-28-2023, 04:14 PM
Last Post: snippsat
  Insert header at first row, for existing csv mg24 2 2,097 Oct-05-2022, 07:12 AM
Last Post: mg24
  Insert a multiple constant value after header in csv file using python shantanu97 1 1,159 Apr-24-2022, 10:04 AM
Last Post: Pedroski55
Question Working with existing files Gilush 7 3,048 Feb-10-2021, 08:55 PM
Last Post: Gilush
  Insert using psycopg giving syntax error near "INSERT INTO" olgethorpe 4 15,663 Jul-21-2017, 07:39 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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