2017-02-09 34 views
0

的第一個和最後一個項目我有過期OSM Tilenames這個排序列表從CSV文件Python-獲取序列

15,17485,11075 
15,17485,11076 
15,17485,11077 
15,17485,11078 
15,17485,11079 
15,17485,11080 
15,17486,11068 
15,17486,11069 
15,17486,11070 
15,17486,11071 
15,17486,11072 
15,17486,11073 
15,17486,11074 
15,17486,11075 
15,17486,11076 
15,17486,11077 
15,17486,11078 
15,17486,11079 
15,17486,11080 
15,17487,11068 
15,17487,11069 
15,17487,11070 
15,17487,11071 
15,17487,11072 
15,17487,11073 
15,17487,11074 
15,17487,11075 
15,17487,11076 
15,17487,11077 
15,17487,11078 
15,17487,11079 

我想每個序列的第一個和最後一個項目,在第三列和第二列的相應條目中創建一個用mapnik進行渲染的邊界框。我不想使用mod_tile。

我沒有問題與第二列提取:

for x_idx, row in enumerate(zoom_15): 
    this_Xelement = row 
    next_Xelement = zoom_15[(x_idx + 1) % len(zoom_15)] 
    X = int(next_Xelement[1]) - int(this_Xelement[1]) 
    x_start = 0 
    x_end = 0 
    y_end = 0 
    y_start = int(this_Xelement[2]) 
    if X == 0: 
     continue 
    elif X == 1: 
     x_start = int(this_Xelement[1]) 
     x_end = int(next_Xelement[1]) 
    elif X < 0: 
     x_start = int(this_Xelement[1]) 
     x_end = int(this_Xelement[1]) + 1 
    elif X > 1: 
     x_start = int(this_Xelement[1]) 
     x_end = int(this_Xelement[1]) + 1 

    print (x_start, x_end) 
    print "++++++++++++++++" 

創建像一些輸出:

enter image description here

但我不能讓第三列重複右鍵,以獲得BB的正確座標。 Im'working關於Python 2.7

更新:

我想everey序列在它的第一個和最後一個條目。 做出來的這個:

15,17485,11075 
15,17485,11076 
15,17485,11077 
15,17485,11078 
15,17485,11079 
15,17485,11080 

我想:

17485,11075 
17485,11080 

回答

0
for x_idx, row in enumerate(zoom_15): 
    this_Xelement = row 
    next_Xelement = zoom_15[(x_idx + 1) % len(zoom_15)] 
    X = int(next_Xelement[1]) - int(this_Xelement[1]) 
    if X == 0: 
     continue 
    elif X == 1: 
     x_start = int(this_Xelement[1]) 
     x_end = int(next_Xelement[1]) 
     y_end = int(this_Xelement[2]) + 1 
     y_start = int(next_Xelement[2]) 
    elif X < 0: 
     x_start = int(this_Xelement[1]) 
     x_end = int(this_Xelement[1]) + 1 
     y_end = int(this_Xelement[2]) + 1 
    elif X > 1: 
     x_start = int(this_Xelement[1]) 
     x_end = int(this_Xelement[1]) + 1 
     y_end = int(this_Xelement[2]) + 1 
     y_start = int(next_Xelement[2]) 

    print(x_start, y_start) 
    print(x_end, y_end) 
    print "+++++++++++" 

這一情況啥子我想:

enter image description here

得到的幫助下我的好學生。 感謝您的幫助!

0

我不知道如果我完全理解你的問題,但我認爲你可以做這樣的事情:

firstRow = None 
lastRow = None 
with open('csvfile.csv','r') as file: 
    for line in file: 
     line = line.strip() 
     if len(line)==0: 
      continue # ignore empty lines, in case they exist 
     if firstRow is None: 
      firstRow = line.strip() # will only be updated once (in the first row) 
     lastRow = line.strip() # will be replaced every time (and only the last row will survive, in the end) 


for line in [firstRow,lastRow]: 
     items = line.split(',') 
     second_item = int(items[1]) 
     third_item = int(items[2]) 
     print (second_item,third_item) 

或使用你的代碼相同的策略:

firstRow = None 
lastRow = None 
for x_idx, row in enumerate(zoom_15):   
    this_Xelement = row 
    next_Xelement = zoom_15[(x_idx + 1) % len(zoom_15)] 
    X = int(next_Xelement[1]) - int(this_Xelement[1]) 
    x_start = 0 
    x_end = 0 
    y_end = 0 
    y_start = int(this_Xelement[2]) 
    if X == 0: 
     continue 
    elif X == 1: 
     x_start = int(this_Xelement[1]) 
     x_end = int(next_Xelement[1]) 
    elif X < 0: 
     x_start = int(this_Xelement[1]) 
     x_end = int(this_Xelement[1]) + 1 
    elif X > 1: 
     x_start = int(this_Xelement[1]) 
     x_end = int(this_Xelement[1]) + 1 
    if firstRow is None: 
     firstRow = (x_start, x_end) 
    lastRow = (x_start, x_end) 

print firstRow 
print lastRow 
print "++++++++++++++++" 
0

我建議使用pandas這個包,它有一個read_csv函數,它有一些強大的數組/表格操作工具。

代碼示例:

import pandas as pd 

data = pd.read_csv(myCsvFile, header=None) 
print(data.loc[:, 1:3]) # get all rows, columns >= 1 and < 3 

導致:

 1  2 
0 17485 11075 
1 17485 11076 
2 17485 11077 
3 17485 11078 
... 
27 17487 11076 
28 17487 11077 
29 17487 11078 
30 17487 11079