0

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??

2
  • 1
    When you say "importing" what do you mean by that, to read into a data-structure like other files? To actually use the import statement? Something else? And what have you tried? Doesn't Python come with a CSV module as standard? Commented Jan 2, 2021 at 7:22
  • 2
    Also please don't tag unrelated languages (or unrelated tags at all). Commented Jan 2, 2021 at 7:23

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

please accept as answer if it solves your question.
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?
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-python
1

PFB 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

Please add your output to your question as text instead of an image. This is important for the search-algorithms.
there you go @wuerfelfreak
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.