1

I have been working on code that takes rows from csv file and transfer them into the lists of integers for further mathematical operations. However, if a row turns out to be empty, it causes problems. Also, the user will not know which row is empty, so the solution should be general rather than pointing at a row and removing it. Here is the code:

import csv
import statistics as st


def RepresentsInt(i):
    try:
       int(i)
       return True
    except ValueError:
       return False
l = []

with open('Test.csv', 'r') as f:
    reader = csv.reader(f, delimiter=',')

    for row in reader:
        l.append([int(r) if RepresentsInt(r) else 0 for r in row])



    for row in l:

        row=[x for x in row if x!=0]
        row.sort()
        print(row)

I've tried l=[row for row in l if row!=[]] and ...

if row==[]:
    l.remove(row)

... but both do nothing, and there is no error code for either. Here is my csv file:

1,2,5,4
2,3
43,65,34,56,7

0,5
7,8,9,6,5

33,45,65,4

If I run the code, I will get [] for row 4 and 6 (which are empty).

9
  • Can you provide a test CSV file? Commented Mar 30, 2017 at 14:51
  • Edited into the question Commented Mar 30, 2017 at 15:14
  • Sorry for bad English. Oh and thanks for edit! Commented Mar 30, 2017 at 15:25
  • 1
    Try swapping for row in reader: with for row in filter(None, reader): Commented Mar 30, 2017 at 15:58
  • Did you have any trouble with my answer? Commented Mar 30, 2017 at 16:50

2 Answers 2

1

This worked on my machine:

import csv

def RepresentsInt(i):
    try:
       int(i)
       return True
    except ValueError:
       return False
l = []

with open('Test.csv', 'r') as f:
    reader = csv.reader(f, delimiter=',')

    for row in reader:
        l.append([int(r) for r in row if RepresentsInt(r)])

    rows = [row for row in l if row]
    for row in rows:
        print(row)
Sign up to request clarification or add additional context in comments.

5 Comments

Unfortunately it does not work. In first case the code runs the same way it did beforehand. In second case I get "'int' object is not iterable" error.
Edited answer, hope its the result.
Can you please add desired output?
Sure. So, if we take values of csv that I put in the question, I want to get:
[1,2,4,5] [2,3] [43,65,34,56,7] [0,5] [7,8,9,6,5] [33,45,65,4]
0

It is unclear what you are doing with the statistics module, but the following program should you what you asked for. The pprint module is imported to make displaying the generated table easier to read. If this answer solves the problem presented in your question but you are having difficulty somewhere else, make sure you open another question targeted at the new problem.

#! /usr/bin/env python3
import csv
import pprint


def main():
    table = []
    # Add rows to table.
    with open('Test.csv', newline='') as file:
        table.extend(csv.reader(file))
    # Convert table cells to numbers.
    for index, row in enumerate(table):
        table[index] = [int(value) if value.isdigit() else 0 for value in row]
    # Remove zeros from the rows.
    for index, row in enumerate(table):
        table[index] = [value for value in row if value]
    # Remove empty rows and display the table.
    table = [row for row in table if row]
    pprint.pprint(table)


if __name__ == '__main__':
    main()

3 Comments

Thank you very much! The code does work just fine. Can you please explain what last 2 lines of your code do?
Also, what if I would like to have zeros as the actual values of my list. I tried to alter the code but it still removes all the zeros.
The last 2 lines make the code importable from another program and prevents this program from executing immediately. Did you read the comments? If you want zeros in your data, then you need to remove lines 14-16 from the code.

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.