Im working on Path Planning of Drone using GPS co ordinates given in .CSV file , How to import GPS co ordintaes from .CSV file to my Python script directly??
3 Answers
taking the locations csv file having lattitute and longitde values as: locations.csv PFB piece of code:
import csv
filename = 'D:\Python\location.csv'
n=0
with open(filename, 'r') as csvfile:
csvreader = csv.reader(csvfile)
fields = next(csvreader)
for row in csvreader:
n = n + 1
print('location {} --> {}:{}\t{}:{}'.format(n,fields[0],row[0], fields[1],row[1]) )
Output:
location 1 --> Latitude:40.741895 Longitude:-73.989308
location 2 --> Latitude:41.741895 Longitude:-72.989308
location 3 --> Latitude:42.741895 Longitude:-71.989308
location 4 --> Latitude:43.741895 Longitude:-70.989308
location 5 --> Latitude:44.741895 Longitude:-74.989308
3 Comments
mukund ghode
please accept as answer if it solves your question.
Kartik Gondhali..
i am new to python... so please can you tell me how to use these values in anather python script? for example if i want to access latitude 40.741895 in my program from .csv file can i access it as latitude[1]=location[1] after executing above lines of program? or is there any other option?
mukund ghode
you can create a list variable like
rows = [] and for each row in csvreader, you can keep on appending the values in rows list (rows.append(row)). Finally you will have a list of list like [[lat1,long1],[lat2,long2],...], then you can itterate and take lattitude and longitudes as required. for more details, please have a look at: geeksforgeeks.org/working-csv-files-pythonPFB sample code:
import csv
filename = 'D:\Python\location.csv'
rows = []
with open(filename, 'r') as csvfile:
csvreader = csv.reader(csvfile)
fields = next(csvreader)
print(fields)
for row in csvreader:
print(row)
rows.append(row)
print(rows)
output:
['Latitude', 'Longitude']
['40.741895', '-73.989308']
['41.741895', '-72.989308']
['42.741895', '-71.989308']
['43.741895', '-70.989308']
['44.741895', '-74.989308']
[['40.741895', '-73.989308'], ['41.741895', '-72.989308'], ['42.741895', '-71.989308'], ['43.741895', '-70.989308'], ['44.741895', '-74.989308']]
2 Comments
wuerfelfreak
Please add your output to your question as text instead of an image. This is important for the search-algorithms.
xyz
there you go @wuerfelfreak
You may have a look at pandas in case you want to do more than merely iterate through the data: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html:
pd.read_csv('data.csv')
As a further extension there's also geopandas that is supposed to make working with geospatial data easier.
importstatement? Something else? And what have you tried? Doesn't Python come with a CSV module as standard?