Skip to content

Instantly share code, notes, and snippets.

@pfuntner
Last active May 16, 2023 12:16
Show Gist options
  • Save pfuntner/760601827b5f26aad4c4198db70b0cce to your computer and use it in GitHub Desktop.
Save pfuntner/760601827b5f26aad4c4198db70b0cce to your computer and use it in GitHub Desktop.
Problem reading an Excel file with Python & pandas: keeping numbers as strings with trailing zeros
#! /usr/bin/env python3
import sys
import argparse
import pandas
parser = argparse.ArgumentParser(description=sys.argv[0])
parser.add_argument('path', help='Path to excel file')
args = parser.parse_args()
spreadsheet = pandas.read_excel(args.path, sheet_name=None)
sheet_names = list(spreadsheet.keys())
sheet_name = sheet_names[0]
sheet = spreadsheet[sheet_name]
column_names = list(spreadsheet[sheet_name].axes[1])
columns = {column_name: list(sheet[column_name]) for column_name in column_names}
for row in range(len(columns[column_names[0]])):
for (column_name, column) in columns.items():
cell = columns[column_name][row]
print(f'{row:2} {column_name:10} {type(cell).__name__:6} {cell}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment