Answer the question
In order to leave comments, you need to log in
Search for the desired column in csv. How to implement it algorithmically?
Good afternoon toaster dwellers.
At work, I had to write a simple python script for selecting photos by geotag.
The problem arose further.
From the introductory:
A directory with a bunch of photos.
CSV file, where there are several columns, the first one is always the name of the photo file.
And somewhere in the rest there are lat and lon coordinates (they always stand like this in a pair, first lat, then lon)
And now, the columns are sometimes mixed up, for example, in one CSV photoname.jpg,lat,lon,,,,
and in another photoname.jpg,angleX,angleY,lat,lon,,,
So, are there any ideas how at the CSV parsing stage to make it so that he himself found the columns with coordinates?
At the output I want to get a list like
[[photoname,lat,lon],[photoname,lat,lon],[photoname,lat,lon],....]
Answer the question
In order to leave comments, you need to log in
You need for each line you need to get lat,lon via findall() and a regexp of the form ,(-?\d{1,2}\.\d{6,},-?\d{1,2}\.\d{6,}),
( https://regex101.com/r/dUucUj/2) , which relies only on 2 consecutive numbers and containing at least 6 characters after comma.
I recommend using pandas and not reinventing the wheel :)
If you have headers in csv, it will determine them and you can access the data directly by the column name.
PS: or the csv module at worst.
In many smart books they write that you need to do a regular expression when
the code has been working for a long time, you want to improve it once, refactor it and forget it.
And the key is that this code will never be read by another programmer again.
I suggest a line when you read immediately into stroka1 = str.split(',') that is, into a list
and then see how many characters after the comma and these columns you name
like
for st in stroka1:
arr = st.split('.')
if len(arr) > 1:
if len(arr[1]) > 5:
print('lat or lon')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question