1

I am having some problems getting the row values from the CSV file below

CSV
minzoom, maxzoom
0,5
5,10
10,18

My Code :

i = 0
for line in open("C:/Marine/lookup/distinct_lookup_scales.csv"):
    i = i + 1
    if (i > 1):  #Skip header
        print("Line: " + line)
        #csv_row = line.split(',')
        minzoom = str(line[0])
        maxzoom = str(line[2])
        print("Minzoom:" + minzoom)
        print("Maxzoom:" + maxzoom)
        readShpFile(minzoom, maxzoom)

The values returned for minzoom and maxzoom has been

0   5
5   1
1   ,

I had used line split but reverted to trying to get items from the line Not sure if that was the best approach

4
  • 2
    What you're reading from csv is a string. Indexing won't work. Split will be a good approach. If you have issues with it, post the output or the issue, or both. Commented Oct 4, 2018 at 9:59
  • 2
    Have you considered using the csv module for this? Commented Oct 4, 2018 at 10:01
  • 1
    minzoom, maxzoom = line.split(',')? Indexing doesn't work, as the zoom can be more than 10 which means you need two characters instead of one and you' may need to shift the max zoom location. Commented Oct 4, 2018 at 10:05
  • Why don't you directly use pandas data frame's read_csv method? Commented Oct 4, 2018 at 10:06

2 Answers 2

3

That is not how you should read the csv file. Take a look at the csv module documentation.

One example :

import csv

with open('C:/Marine/lookup/distinct_lookup_scales.csv', 'r') as csvfile:
    csvreader = csv.reader(csvfile)
    csvreader.next() #skip header
    for row in csvreader:
        minzoom = int(row[0])
        maxzoom = int(row[1])
        print('minzoom : {}'.format(minzoom))
        print('maxzoom : {}'.format(maxzoom))

You can also use a DictReader which will use your header line to yield dictionaries.

import csv

with open('C:/Marine/lookup/distinct_lookup_scales.csv', 'r') as csvfile:
    csvreader = csv.DictReader(csvfile)
    for row in csvreader:
        minzoom = int(row['minzoom'])
        maxzoom = int(row['maxzoom'])
        print('minzoom : {}'.format(minzoom))
        print('maxzoom : {}'.format(maxzoom))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Corentin!. I tried the first way above and the readShpFile method now works as expected!. Thank you so much! Dave
Thanks also Corentin for the CSV module documentation link, Much appreciated!
0

You can try numpy.genfromtxt, like:

import numpy as np

data = np.genfromtxt("C:/Marine/lookup/distinct_lookup_scales.csv", delimiter = ",",
                     skip_header = 1)
minzooms = data[:,0]
maxzooms = data[:,1]

1 Comment

Thanks Y.Wang. Not used numpy before as I am new to python but will give the approach a try. Dave

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.