0

I'm currently working on a project and got stuck here. I've got a csv file that has 8 rows of data

  1. row 1
  2. row 2
  3. row 3
  4. row 4
  5. row 5
  6. row 6
  7. row 7
  8. row 8

I'm trying to make it to read the first 4 rows, write them to a new csv file, then do the same with with the next 4 rows of data. So I thought if I code it to find the total rows of data in the original csv, then divide it by 4, and make my code to read the first pack of data, then read the second pack of data, Currently it's just reading the whole 8 rows of data again again and again, now I'm just not even sure if i'm coding it towards the correct direction

import csv
with open('a.csv','r') as e:
    csv_raw=csv.reader(e)
    csv_raw_cont = list(csv_raw)
    value = len(csv_raw_cont)
    print(value)
    forum_post_numbers = int(value/4)
    print(forum_post_numbers)
    e.seek(0)
    for j in range(0,forum_post_numbers):
        for i in range(0,value):
            print(csv_raw_cont[i])
  • 1
    Why not read it all once from input and then open output files one by one and write to them what needs to be written? – zvone Nov 02 '20 at 02:20
  • https://stackoverflow.com/questions/4956984/how-do-you-split-reading-a-large-csv-file-into-evenly-sized-chunks-in-python - this might help. – BernardL Nov 02 '20 at 04:19
  • @BernardL Thanks! that's exactly what i'm looking for! I didn't even know how to ask the question properly ahhaha thanks! – I suck at this Nov 02 '20 at 10:43
  • @zvone hmm...that could work, the problem is, i keep getting row1 to row8 repeated again and again and again instead of having it chunked. – I suck at this Nov 03 '20 at 02:46

0 Answers0