-1

I'm trying to read the CSV file only if has between 200 and 400 records. I want it to ignore otherwise. For some weird reason my code never reaches the print(row) line

with open(file) as csv_file:
    row_count = sum(1 for line in csv_file)
    if (row_count>200 and row_count<400):
        csv_reader = csv.reader(csv_file, delimiter=',')
        for row in csv_reader:
            print(row)
Lambo
  • 1,094
  • 11
  • 18

2 Answers2

0

You forgot to read the data from the file. To get to the data you need to csv_file.read() Even so, there's a lot of hassle as to the handling of the file.

I suggest using the pandas module because of its simplicity.

import pandas as pd

data =  pd.read_csv(r'filepath')
if 200<len(data)<400:
    print('True',len(data))
Cheng An Wong
  • 308
  • 2
  • 8
0

I`m still trying to figure out why, but you need to read the file again after performing the line count.

with open(file) as csv_file:
    row_count = sum(1 for line in csv_file)

if (row_count > 200 and row_count < 400):
    with open(file) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        for row in csv_reader:
            print(row)
Lambo
  • 1,094
  • 11
  • 18
  • Thanks snakecharmerb, that explains it - csv_file is an iterator; after you loop over it with sum it's exhausted – Lambo Apr 16 '20 at 07:18
  • Also a good answer available here - >https://stackoverflow.com/a/27504076/4285029 – Lambo Apr 16 '20 at 07:20