Skip to content

Commit

Permalink
Add Examples in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Mar 1, 2015
1 parent b3164bc commit 04ce128
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
54 changes: 54 additions & 0 deletions docs/examples/write to csv.md
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)
```
4 changes: 4 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ pages:
- ['features/Confidence Score.md', 'Features', 'Confidence Score']
- ['features/Well-Known Text.md', 'Features', 'Well-Known Text']

# Examples
# ========
- ['examples/write to csv.md', 'Examples', 'Write to CSV']

# Providers
# =========
- ['providers/Yandex.md', 'Providers', 'Yandex']
Expand Down

0 comments on commit 04ce128

Please sign in to comment.