0

I have 2 files " CSV "

i want to insert the lines from the first file into the second file

how to insert specific numbers of lines like example the first 10 lines and also how to insert the all file

how to do this with python ?

for example :

First file include :

1 , A
2 , B
3 , C

Second file include :

4 , D

i want to add the lines from the first file to the second file so second file will be like this :

4 , D
1 , A
2 , B
3 , C

This is the code i use :

outfile = open("second.csv", "w", encoding="utf8")
for line in open("first.csv", "r", encoding="utf8"):
     outfile.write(line)
outfile.close()

But the problem in my code is not insert more lines the code delete what in second file and then insert into it what in the first file ( what i want is to insert lines without delete what was in the second file )

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
MOHA7z
  • 31
  • 8

2 Answers2

0

See this post on reading CSV line by line:

Reading rows from a CSV file in Python

If you map the content to a pandas object, you can do this:

grid.to_csv('output.csv')
Ian Heisler
  • 111
  • 6
0

This is the solution

outfile = open("second.csv", "a", encoding="utf8")
for line in open("first.csv", "r", encoding="utf8"):
     outfile.write(line)
outfile.close()
MOHA7z
  • 31
  • 8