我从 CSV 文件中获得了已过期 OSM Tilenames 的已排序列表:
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 "++++++++++++++++"
创建一些输出,例如:
但我不能让第三列迭代正确,以获得BB的正确坐标。 我正在使用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 :(得分: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 "++++++++++++++++"
答案 1 :(得分: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
答案 2 :(得分: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 "+++++++++++"
这正在创造我想要的东西:
在我的优秀学生的帮助下得到了。 感谢您的帮助!!!