by mikub · 5y ago · 0 comments
import csv
import readline
readline.set_completer_delims(' \t\n=')
readline.parse_and_bind("tab: complete")
def csvOpen(file):
with open(file, "r", newline='', encoding='utf-8-sig') as csvfile:
reader = csv.reader(csvfile)
i = next(reader)
dRows = sum(1 for line in reader)
print(f"You have {dRows} rows in the CSV file:\n" f"Your headers are:\n {i}")
file = input("Please enter the filename to show the CSV headers: ")
csvOpen(file)
I also have an alias for it in my .bash_aliases. alias sqhead='python /home/user/Coding/Py-Code/sqlHead.py' import sys
file = sys.argv[1]
instead of call to input()You could probably do:
if len(sys.argv) > 1:
file = sys.argv[1]
else:
file = input(...)- `head -n 1 <file>` to get headers - `wc -l <file>` to count number of rows