2017-02-09 37 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