import os
import csv
def get_file_path(filename):
currentdirpath = os.getcwd()
file_path = os.path.join(os.getcwd(), filename)
print(file_path)
return(f
If you would consider using pandas, read_csv
makes reading files very straightforward:
import pandas as pd
data = pd.read_csv(filename, skiprows=9)
If you are using numpy, have a look at the skip_header argument in genfromtxt (http://docs.scipy.org/doc/numpy/user/basics.io.genfromtxt.html )
import numpy as np
r = np.genfromtxt(filepath, skip_header=9, names = ['account','amount'] , delimiter = ',')
print(r.account[0],r.amount[0])
You can use itertools.islice() to skip a fixed number of lines:
from itertools import islice
next(islice(reader, 9, 9), None)
for row in reader:
print(row[0])
The islice()
object is instructed to skip 9 lines, then immediately stop without producing further results. It is itself an iterator, so you need to call next()
on it still.
If you wanted to skip rows until the 'empty' row, that requires a different approach. You'd have to inspect each row and stop reading when you come across one that has only empty cells:
for row in reader:
if not any(row): # only empty cells or no cells at all
break
for row in reader:
print(row[0])
Demo of the latter approach:
>>> import csv
>>> import io
>>> sample = '''\
... Summary Journal Entry,JE-00000060
... Journal Entry Date,28/02/2015
... Accounting Period,Feb-15
... Accounting Period Start,1/02/2015
... Accounting Period End,28/02/2015
... Included Transaction Types,Invoice Item
... Included Time Period,01/02/2015-09/02/2015
... Journal Run,JR-00000046
... Segments,
... ,
... Customer Account Number,Transaction Amount
... 210274174,545.45
... 210274174,909.09
... 210274174,909.09
... 210274174,909.09
... 210274174,909.09
... '''
>>> with io.StringIO(sample) as csvfile:
... reader = csv.reader(csvfile)
... for row in reader:
... if not [c for c in row if c]:
... break
... for row in reader:
... print(row[0])
...
Customer Account Number
210274174
210274174
210274174
210274174
210274174
Note that you want to leave newline handling to the csv.reader
; when opening your file set newline=''
:
with open(filepath, 'r', newline='') as csvfile: