Skip to content

Instantly share code, notes, and snippets.

@JulianWgs
Created March 28, 2020 10:23
Show Gist options
  • Save JulianWgs/9a31b5c863308019abe07d491e89ed20 to your computer and use it in GitHub Desktop.
Save JulianWgs/9a31b5c863308019abe07d491e89ed20 to your computer and use it in GitHub Desktop.
Converting pip list --outdated to pandas dataframe
# This is also applicable to other table which are styled similar.
import pandas as pd
with open("packagelist.txt") as file:
input_str = file.read()
column_widths = [len(dashes) for dashes in input_str.split("\n")[1].split()]
column_names = input_str.split("\n")[0].split()
data = {key: list() for key in column_names}
for line in input_str.split("\n")[2:]:
start = 0
end = 0
for column_name, column_width in zip(column_names, column_widths):
end += column_width + 1
data[column_name].append(line[start:end])
start = end
pd.DataFrame(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment