[英]Convert a database of lat long co-ordinates into utm data in Python
I'm trying to convert a csv set up as [name | 我正在尝试将设置为[名称| lat |
lat | long] into a new csv set out as [name |
long]转换为新的csv,名称为[name | easting |
东 northing |
北 zone |
区| --], but it keeps coming up with the error;
-],但它会不断提出错误;
Traceback (most recent call last): File "E:\\ACES Mapping Project\\Data\\Latlong_data\\utm_request.py", line 25, in company = eachline[0] IndexError: list index out of range
追溯(最近一次呼叫最近):文件“ E:\\ ACES Mapping Project \\ Data \\ Latlong_data \\ utm_request.py”,公司中的第25行= eachline [0] IndexError:列表索引超出范围
I'm new to Python but had no problems when converting the original database of addresses and postcodes into lat and long with a very similar code so I'm unsure of what I've done wrong? 我是Python的新手,但是将原始地址和邮政编码数据库转换为经纬度很长的lat时却没有问题,因此代码非常相似,所以我不确定自己做错了什么吗? Any help would be great thank you, I'm sorry if this is really obvious.
任何帮助都将非常感谢,如果确实如此,对不起。 My code is below.
我的代码如下。
import csv
import utm
# Here enter the lat and long column numbers;
lat_col = 1
long_col = 2
# Here enter the name of the csv file for reading (r) and writing (w)
reading = 'Eng_latlong_data.csv'
writing = 'eng_utm.csv'
rawdata = open(str(reading),'r')
csv1 = csv.reader(rawdata,delimiter='|')
newdata = open(str(writing),'w')
csv2 = csv.writer(newdata,delimiter='|')
# below code will read through each line in lat long file
# then convert to utm
# and finally write as a new line with 'company name', (easting, northing, zone number, zone letter)
for eachline in csv1:
company = eachline[0]
print(company)
try:
u = utm.from_latlon(float(eachline[1]), float(eachline[2]))
csv.writerow([company, u[0], u[1], u[2], u[3]])
except:
print(' ...error')
rawdata.close()
newdata.close()
This is my fixed code, I'm sure it could be done with less but if it helps anyone, I just had blank lines in the csv file, which was fixed using the if and else statement. 这是我的固定代码,我相信可以用更少的钱完成它,但是如果对任何人都有用,我在csv文件中只是空行,使用if and else语句进行了修复。 The if len() > 1 runs through the code only if the length of that line is more than 1, else it doesn't.
仅当该行的长度大于1时,if len()> 1才会遍历代码。
import csv
import utm
# Here enter the lat and long column numbers;
lat_col = 1
long_col = 2
# Here enter the name of the csv file for reading (r) and writing (w)
reading = 'SIA_latlong_data.csv'
writing = 'SIA_utm.csv'
rawdata = open(str(reading),'r')
csv1 = csv.reader(rawdata,delimiter='|')
newdata = open(str(writing),'w')
csv2 = csv.writer(newdata,delimiter=',')
# below code will read through each line in lat long file
# then convert to utm
# and finally write as a new line with 'company name', (easting, northing, zone number, zone letter)
for eachline in csv1:
if len(eachline) > 1:
try:
company = eachline[0]
print(company)
u = utm.from_latlon(float(eachline[lat_col]), float(eachline[long_col]))
csv2.writerow([str(company), str(u[0]), str(u[1]), str(u[2]), str(u[3])])
print('written')
except:
print(' ...ERROR')
else:
print(' ...blank line')
rawdata.close()
newdata.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.