0

So this is my code:

import csv

particle_counter = file('C:/Users/Desktop/Level 2  files/HiSAM1_data_160206_134230.csv','rU')
gas_csv = file('C:/Users/Desktop/Level 2 files/gas_0.csv','rU')
gps_csv = file('C:/Users/Desktop/Level 2 files/gps_0.csv','rU')
pm2_5_csv = file('C:/Users/Desktop/Level 2 files/pm25_0.csv','rU')


reader1 = csv.reader(particle_counter)
reader2 = csv.reader(gas_csv)
reader3 = csv.reader(gps_csv)
reader4 = csv.reader(pm2_5_csv)

def skipline(n,filename):
    x =0
    while x < n in filename:
        reader = csv.reader(filename)
        result = next(reader)
        return result


print(skipline(0,particle_counter) ) 

I've been trying to know how do you skip headers to files that have different number of headers in one single function. However, I think i got what i need in order to do that but when i run the program i get:

In[21]: %run "C:/Users/Desktop/Level 2 files/skiplinefunction.py"   
None

What i expected to happen was that the function would take each file and skip the number of header lines that each individual file has until it reaches values. I tried printing one file to see if it skiped the lines on that file but i got the None. What does that mean? is there anything wrong in my program that I can't seem to see? How can i fix this?

user665997
  • 313
  • 1
  • 4
  • 18
  • 1
    Please do add what you *expected* to happen instead. What is the function of `skipline()`? – Martijn Pieters Jul 12 '16 at 18:08
  • @MartijnPietersI just updated the question with what I expect to happen in the function :) – user665997 Jul 12 '16 at 18:11
  • That's not a very clear explanation. Did you want to skip `n` lines, or *up to* `n` lines *or* if there is a non-empty line? – Martijn Pieters Jul 12 '16 at 18:12
  • @MartijnPieters i wanted to skip n lines of headers in the file until it reached values, so skip headers and stop at values. I don't know if that made sense LOL – user665997 Jul 12 '16 at 18:13
  • So you know how many headers there are to skip? – Martijn Pieters Jul 12 '16 at 18:14
  • Did you see [Skip the headers when editing a csv file using Python](http://stackoverflow.com/q/14257373) and [Python CSV reader to skip 9 headers](http://stackoverflow.com/q/28603014) yet? – Martijn Pieters Jul 12 '16 at 18:14
  • @MartijnPieters i do know how many headers are to skip in each file. I know how to skip lines in a file this way: particle_counter = file('C:/Users/Alan Cedeno/Desktop/Level 2 files/HiSAM1_data_160206_134230.csv','rU') reader1 = csv.reader(particle_counter) header1 = reader1.next() for row in reader1: print row – user665997 Jul 12 '16 at 18:17
  • @MartijnPieters but other files have 3 or 5 different headers. So i figured that instead of putting a next function on top of each for look, i could just make a function to do the same work. – user665997 Jul 12 '16 at 18:18
  • Right, then just use the `islice()` option I explain in my answer here and in the other question. – Martijn Pieters Jul 12 '16 at 18:19
  • @MartijnPieters so only one line of headers would be next(islice(reader1, 0, 0), None) ? – user665997 Jul 12 '16 at 18:25
  • No, you are skipping *1* line, so use `next(islice(reader1, 1, 1), None)`. – Martijn Pieters Jul 12 '16 at 18:26

2 Answers2

2

skipline() returned it, and you printed it. And skipline() returned it because your while test can never be True:

while x < n in filename:

The expression x < n in filename is a chained comparison, and is executed as x < n and n in filename. x < n is already false since both x and n are both 0, but the test n in filename is also false because 0 is never going to be an element in a string sequence.

The function then just ends without an explicit return, so the default return None is executed instead, and you print that result.

If you wanted to skip a certain number of rows in your CSV file, do so directly on either the open file object (to skip lines that the CSV reader should not parse) or on the reader objects. You don't need to pass in a filename for that. Use the consume recipe from the itertools recipes section; for your case that comes down to using next(islice(iterator, n, n), None), see Python CSV reader to skip 9 headers.

Applied to your function that'd be:

from itertools import islice

def skiplines(reader, n):
    next(islice(reader, n, n), None)

and use this directly on the reader objects:

skiplines(particle_counter, 1)

The function does not return anything.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • @gokublack: I stated in my answer why: this function doesn't return anything. The whole point of the function is to skip a certain number of lines. If you wanted to read those lines and print them, just do so. – Martijn Pieters Jul 12 '16 at 18:32
  • @martin Pieters i'm still trying to figure out what you said hahaha is not easy, i just been coding python for not too long. Could you show me an example of one file so i can do the rest? :D – user665997 Jul 12 '16 at 18:48
  • @gokublack: `for i in range(5): print next(particle_counter)`? Or `print list(islice(particle_counter, 5))` to get up to five rows into a list and print those? – Martijn Pieters Jul 13 '16 at 13:13
0

In the skipline() function:

def skipline(n,filename):
    x =0
    while x < n in filename:
        reader = csv.reader(filename)
        result = next(reader)
        return result

x = 0 and n = 0. So nothing is being parsed as x < n evaluates to False. The function thus returns the default value of None.

Vaibhav Bajaj
  • 1,934
  • 16
  • 29