Skip to content

Commit d3d076f

Browse files
committed
Add pretty CSV script
1 parent 4339f92 commit d3d076f

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

Pretty-CSV/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Pretty CSV
2+
3+
This script pretty-prints CSV input into a table output for easier readibility. The script reads from stdin and writes into stdout for pipe compatibility.
4+
5+
## Examples
6+
Read from local file
7+
```sh
8+
python3 pretty-csv.py < csv-file.csv
9+
```
10+
11+
Read from `curl`
12+
```sh
13+
curl -fsSL https://people.sc.fsu.edu/~jburkardt/data/csv/cities.csv | python3 pretty-csv.py
14+
```
15+
16+
Pipe to `less` for better navigation
17+
```sh
18+
python3 pretty-csv.py < long-csv-file.csv | less -S
19+
```

Pretty-CSV/pretty-csv.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python3
2+
import csv
3+
import sys
4+
from typing import Iterable, List
5+
6+
7+
def main():
8+
content_lines = sys.stdin.buffer.readlines()
9+
reader = csv.reader(line.decode('utf-8') for line in content_lines)
10+
headers = next(reader)
11+
print(create_table(reader, headers))
12+
13+
14+
def create_table(rows: Iterable[List[str]], headers: List[str]) -> str:
15+
table = [headers]
16+
column_lengths = [len(header) for header in headers]
17+
for row in rows:
18+
for i, text in enumerate(row):
19+
column_length = column_lengths[i]
20+
text_length = len(text)
21+
if text_length > column_length:
22+
column_lengths[i] = text_length
23+
table.append(list(row))
24+
25+
result = []
26+
for row in table:
27+
row_text = []
28+
for i, text in enumerate(row):
29+
column_length = column_lengths[i]
30+
row_text.append(space_pad(text, column_length))
31+
result.append(' '.join(row_text))
32+
return '\n'.join(result)
33+
34+
35+
def space_pad(text: str, length: int) -> str:
36+
temp = text + ''.join(' ' for _ in range(length))
37+
return temp[:length]
38+
39+
40+
if __name__ == '__main__':
41+
main()

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ So far, the following projects have been integrated to this repo:
7272
|[Yoda-speak Translator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/speak_like_yoda)|[sonniki](https://github.com/sonniki) |
7373
|[Medium Article Downloader](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/medium_article_downloader)|[coolsonu39](https://github.com/coolsonu39)|
7474
|[Clean_up_photo](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Clean_up_photo)|[sritanmay001](https://github.com/sritanmy001)|
75+
|[Pretty CSV](https://github.com/Frizz925/Awesome-Python-Scripts/tree/pretty-csv/Pretty-CSV)|[Frizz925](https://github.com/Frizz925)|
7576
## How to use :
7677

7778
- Clone/Download the directory and navigate to each folder. Or...

0 commit comments

Comments
 (0)