-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b3164bc
commit 04ce128
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Write to CSV | ||
|
||
## Single Address | ||
|
||
```python | ||
import geocoder | ||
import csv | ||
|
||
g = geocoder.bing('Avenida da Republica, Lisboa') | ||
|
||
with open('test.csv', 'wb') as f: | ||
writer = csv.DictWriter(f, fieldnames=g.fieldnames) | ||
writer.writeheader() | ||
writer.writerow(g.json) | ||
``` | ||
**Note**: The `fieldnames` is new in 1.1.3, `attributes` in the earlier versions. | ||
|
||
## Multiple Address | ||
|
||
The CSV file used for **locations.csv**. | ||
|
||
Using `delimiter` for parsing a CSV might include `<\t> <;> <|> <,>` | ||
|
||
|
||
| location | extra | | ||
|-------------|-----------| | ||
| Canada | best | | ||
| Australia | amazing | | ||
| Venezuela | awesome | | ||
|
||
```python | ||
import geocoder | ||
import csv | ||
|
||
rows = [] | ||
fieldnames = ['location', 'extra', 'lat', 'lng', 'state', 'country'] | ||
|
||
with open('locations.csv') as f: | ||
reader = csv.DictReader(f, delimiter=';') | ||
for line in reader: | ||
g = geocoder.google(line['location']) | ||
|
||
# Add the CSV line data into the Geocoder JSON result | ||
result = g.json | ||
result.update(line) | ||
|
||
# Store Geocoder results in a list to save it later | ||
rows.append(result) | ||
|
||
with open('locations_new.csv', 'wb') as f: | ||
writer = csv.DictWriter(f, fieldnames=fieldnames, extrasaction='ignore') | ||
writer.writeheader() | ||
writer.writerows(rows) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters