0

I want to read several lines of an CSV file. I am opening a list and append the one row to the list. Then I try to print the list. But the list is empty. The CSV file looks as following:

`hallo;das;ist;ein;test;der;hoffentlich;funktioniert;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert1;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert2;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert3;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert4;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert5;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert6;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert7;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert8;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert9;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert10;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert11;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert12;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert13;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert14;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert15;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert16;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert17;fingerscrossed;
`

This is my Code:

import csv
spamreader = csv.reader(open('test.csv'), delimiter = ';')

verbraeuche_ab_reset = []


def berechne_gemittelten_verbrauch():
    anzahl_zeilen = sum(1 for row in spamreader)
    for row in spamreader:
        if spamreader.line_num > 9 and spamreader.line_num < anzahl_zeilen:
            verbrauch_ab_reset = row[7]

            verbraeuche_ab_reset.append(verbrauch_ab_reset)

    print(verbraeuche_ab_reset)
    print(anzahl_zeilen)

berechne_gemittelten_verbrauch()

Thx in advance!

  • Remove the `if` statement at first. `line_num` dos not count the fields, which I assume you think – tuergeist Sep 02 '16 at 09:08
  • You have already consumed the file in calculating `anzahl_zeilen`, so there is nothing left for `for row in spamreader` to loop over. – jonrsharpe Sep 02 '16 at 09:11
  • Can you explain your goal more precisely? You say you want to read several lines of a csv file, but I have problems to understand what you are actually trying to do in your code snippet there... – a.smiet Sep 02 '16 at 09:13

3 Answers3

0

The following works. Remind, iterating over the data in the line anzahl_zeilen ... makes it impossible to iterate again over your data.

2nd thing. if spamreader.line_num > 9 and spamreader.line_num < anzahl_zeilen: does neither really check the columns in the row nor if you're at the end. The iterator does the latter one for you. To cound the columns, use len(row) instead.

import csv
spamreader = csv.reader(open('test.csv'), delimiter = ';')


def berechne_gemittelten_verbrauch():
    #anzahl_zeilen = sum(1 for row in spamreader) # kills your data / iterator is at the end
    verbraeuche_ab_reset = []
    for row in spamreader:
        if len(row) > 9:
           verbrauch_ab_reset = row[7]
        verbraeuche_ab_reset.append(verbrauch_ab_reset)
    return verbraeuche_ab_reset

verb = berechne_gemittelten_verbrauch()
# subsets
print(verb[9:11]) 

Please read python subset notation

a[start:end] # items start through end-1
a[start:]    # items start through the rest of the array
a[:end]      # items from the beginning through end-1
a[:]         # a copy of the whole array
Community
  • 1
  • 1
tuergeist
  • 9,171
  • 3
  • 37
  • 58
  • It still doesn´t do what I want. Now the list includes empty contents as long as the list is. So yes it iterates over the data but does not get any content and does not only reads the specific lines I want it to read. – Johannes Wiese Sep 03 '16 at 08:05
  • First, this answer solves your question. You have not specified, that you're interested in specific lines. But you can du this easily. Answer edited. – tuergeist Sep 06 '16 at 08:14
0

The problem with your code is that you are iterating over the spamreader twice. You can only do that once.

this statement will result in the correct answer.

anzahl_zeilen = sum(1 for row in spamreader)

but when you now iterate over the same spamreader you'll get empty list, since you have already iterated over the file once

for row in spamreader:
    if spamreader.line_num > 9 and spamreader.line_num < anzahl_zeilen:
        verbrauch_ab_reset = row[7]

        verbraeuche_ab_reset.append(verbrauch_ab_reset)

to solve this use,

spamreader = csv.reader(open('test.csv'), delimiter = ';')
    anzahl_zeilen = sum(1 for row in spamreader)
    spamreader = csv.reader(open('test.csv'), delimiter = ';')
        for row in spamreader:
            if spamreader.line_num > 9 and spamreader.line_num < anzahl_zeilen:
                verbrauch_ab_reset = row[7]

                verbraeuche_ab_reset.append(verbrauch_ab_reset)
0
# try this code its very simple
input:
filename :samp1.csv
c1;c2;c3;c4;c5;c6;c7;c8;c9;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert1;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert2;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert3;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert4;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert5;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert6;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert7;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert8;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert9;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert10;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert11;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert12;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert13;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert14;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert15;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert16;fingerscrossed;
hallo;das;ist;ein;test;der;hoffentlich;funktioniert17;fingerscrossed;
#read the file
import pandas as pd
data = pd.read_csv('samp1.csv',sep=';')
df = pd.DataFrame({'c1':data.c1,'c2':data.c2,'c3':data.c3,'c4':data.c4,'c5':data.c5,'c6':data.c6,'c7':data.c7,'c8':data.c8,'c9':data.c9,})

#suppose we want to print first 6 lines
lines = df.ix[:5,['c1','c2','c3','c4','c5','c6','c7','c8','c9']]
print(lines)
output:
      c1   c2   c3   c4    c5   c6           c7             c8              c9
0  hallo  das  ist  ein  test  der  hoffentlich   funktioniert  fingerscrossed
1  hallo  das  ist  ein  test  der  hoffentlich  funktioniert1  fingerscrossed
2  hallo  das  ist  ein  test  der  hoffentlich  funktioniert2  fingerscrossed
3  hallo  das  ist  ein  test  der  hoffentlich  funktioniert3  fingerscrossed
4  hallo  das  ist  ein  test  der  hoffentlich  funktioniert4  fingerscrossed
5  hallo  das  ist  ein  test  der  hoffentlich  funktioniert5  fingerscrossed
SUNITHA K
  • 150
  • 1
  • 3
  • Thank you! But what if I want to print the lines 9 til 11? lines = df.ix[:9-11,['c1','c2','c3','c4','c5','c6','c7','c8','c9']] ? – Johannes Wiese Sep 03 '16 at 07:31