-1

I have 2 CSV files, say 'a.csv' and 'b.csv'. I want to copy the data of 'a.csv' into 'b.csv'. The whole of 'a.csv' data should be added at the end of the 'b.csv'

import csv
ifile = open('/Users/avtarsingh/Downloads/StocksProject-master/data/nasdaq.csv', 'w', newline='')
read = csv.reader(ifile)
writer = csv.writer(ifile)
for row in ifile.readlines():
    writer.writerow(['Avtar'])
    print(row)
ifile.close()

=================================================================

New Code:-

with open('/Users/avtarsingh/Downloads/StocksProject-master/data/procter.csv', 'r') as f:
    # f = list(f)
    for row in f:
        print(row)
time.sleep(2)
with open('/Users/avtarsingh/Downloads/StocksProject-master/data/sp.csv', 'r') as g:
    for row1 in g:
        # g.writerow(f)
        print(row1)
with open('/Users/avtarsingh/Downloads/StocksProject-master/data/nasdaq.csv', 'w') as h:
    writer = csv.writer(h)
    writer.writerows(f)
Anton vBR
  • 18,287
  • 5
  • 40
  • 46
S...R... T
  • 11
  • 1
  • 6

1 Answers1

0

There's a few things that appear to be wrong here. You may want to review the docs for the CSV module. There are likely to be some good examples you can follow.

First, in line 2, if that is the data file that you want to read in, then you need to open it in read mode 'r', not write mode 'w'.

Then depending on how you approach, you will want to open a second file in write mode , 'w'. This is your target file , the file you are copying the data to.

Consult csv module and file opening docs and try again using the suggestions above. If you have problems ask again, giving specific error messages or problems you are encountering and people will be happy to help.

chrisfs
  • 6,182
  • 6
  • 29
  • 35