0

I have a csv file that I want to work with using python. For that I'm run this code :

import csv
import collections

col_values = collections.defaultdict(list)
with open('list.csv', 'rU',) as f:
    reader = csv.reader(f)
    data = list(reader)
    row_count =len(data)
    print(" Number of rows  ", row_count)

As a result I get 4357 but the file has only 2432 rows, I've tried to change the delimiter in the reader() function, it didn't change the result. So my question is, does anybody has an explanation why do I get this value ? thanks in advance

UPDATE

since the number of column is also too big , here is the output of the last row and the start of non existing rows for one columns enter image description here

opening the file with excel the end looks like :

enter image description here

I hope it helps

5
  • Can you post, as an example, a couple of rows from your file? Commented Mar 22, 2017 at 9:11
  • Yes.. Please share some data. I executed code with 10 lines in csv file and result came proper. Commented Mar 22, 2017 at 9:13
  • Read the csv.reader docs, and use 'r',newline='' with open, assuming Python 3. Commented Mar 22, 2017 at 12:25
  • @MarkTolonen didn't solve the issue still getting 4357 rows ! Commented Mar 22, 2017 at 12:29
  • Get a hex editor and see what is wrong with your file, or provide a link to a sample of it. Commented Mar 22, 2017 at 21:23

1 Answer 1

1

try using pandas.

import pandas as pd
df = pd.read_csv('list.csv')
df.count()

check whether you are getting proper rows now

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't want to use panda , because it has a conflict with Tensorflow. But it's a way to do this. I've set a limit for numbers of row

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.