An open API service indexing awesome lists of open source software.

https://github.com/timwis/slugify-headers

A python script to get database_friendly_field_names for a CSV file
https://github.com/timwis/slugify-headers

Last synced: about 2 months ago
JSON representation

A python script to get database_friendly_field_names for a CSV file

Awesome Lists containing this project

README

        

# Slugify Headers
A python script to get database_friendly_field_names for a CSV file

## Usage
Given a CSV file where the first row contains malformed headers, ie.:
```
Animal,No. of Legs, DOMESTICATED, Notes** Here
Dog, 4, true, "great pet"
Cat, 4, false, "no thx"
```

Pass the file to this script via `stdin` and it will return database-friendly headers
with special characters and extra spaces removed:
```bash
$ cat data.csv | python main.py
> animal,no_of_legs,domesticated,notes_here
```

For greater efficiency, just pass the first row of the CSV file:
```bash
$ head -1 data.csv | python main.py
> animal,no_of_legs,domesticated,notes_here
```

To replace the header line in the original file, use silly simple posix programs:
```bash
# Get clean headers and put then in a new file
$ head -1 data.csv | python main.py > new_data.csv

# Append the source file without the first row to the new file
$ tail -n+2 data.csv >> new_data.csv
```

To output the headers as JSON, append the `--json` argument:
```bash
$ head -1 data.csv | python main.py --json
> ["animal", "no_of_legs", "domesticated", "notes_here"]
```