Reputation: 25
everybody,
I hope you can help me with this problem. I currently have a program that reads the coordinates and date from images and writes them to a file called "test.csv".
Here you can find my current code:
IMAGE_DIRECTORY = Path("/path")
images = list(IMAGE_DIRECTORY.glob("*.jpg")) + list(IMAGE_DIRECTORY.glob("*.png"))
i= 1
date_to_latlng = {}
for image in images:
meta_data = ImageMetaData(image)
latlng = meta_data.get_lat_lng()
date = meta_data.get_date_time()
date_to_latlng[date] = [latlng, "string", "some other string", i]
i = i + 1
with open("test.csv", "w", newline="") as csvfile:
writer = csv.writer(csvfile, delimiter=" ")
for date in sorted(date_to_latlng):
writer.writerow(date_to_latlng[date])
Currently the entries are sorted by date and written to the file test.csv.
The output looks like this:
"(52.59504, -126.17413)",some string,some other string, 2
"(43.05749, -121.72245)",some string,some other string, 1
But I would like the output to look like this:
52.59504 -126.17413 some string some other string 1
43.05749 -121.72245 some string some other string 2
Each entry in the test.csv file should have an ascending numbering at the end. Furthermore, the "" quotes and the brackets () should be deleted.
Anyone have any idea how to solve this?
Upvotes: 0
Views: 74
Reputation: 55913
It looks like latlng
is a tuple; unpacking it into two variables will remove the brackets problem. Since we haven't sorted the data we won't try to add the final column yet.
date_to_latlng = {}
for image in images:
meta_data = ImageMetaData(image)
lat, lng = meta_data.get_lat_lng()
date = meta_data.get_date_time()
date_to_latlng[date] = [lat, lng, "string", "some other string"]
Now we can use the enumerate builtin function to set the final column as we iterate over the sorted data.
with open("test.csv", "w", newline="") as csvfile:
writer = csv.writer(csvfile, delimiter=" ")
for i, date in enumerate(sorted(date_to_latlng), start=1):
row = date_to_latlng[date]
row.append(i)
writer.writerow(row)
Note that if you are writing a csv with a space as the column delimiter, values with embedded spaces will be quoted within double-quotes (or the value of quotechar
that you pass to csv.writer).
Upvotes: 1
Reputation: 524
You could do with formatting your latlng
to get the format you would like (two floats).
You could write a helper function like this:
def formatLatLang(latlang):
latlang = latlang.replace('(', '').replace(')', '').split(',')
return [float(latlang[0]), float(latlang[1])]
This would return a list of two floats.
You need to format what you add to date_to_latlng
in order to remove the commas because you're passing in a list. You could pass all this in as a string. If you want to do this, don't parse the strings as floats like in the method above, keep them as strings.
[lat, lang] = formatLatLang(latlng)
date_to_latlng[date] = " ".join([lat, lang, "string", "some other string", i])
However, CSV means comma separated values and this would not make a valid CSV file. You could add a line to a .txt file instead.
Upvotes: 0