vebey74412 :
have this txt files with values
google.com('172.217.163.46', 443)
commonName: *.google.com
issuer: GTS CA 1O1
notBefore: 2020-02-12 11:47:11
notAfter: 2020-05-06 11:47:11
facebook.com('31.13.79.35', 443)
commonName: *.facebook.com
issuer: DigiCert SHA2 High Assurance Server CA
notBefore: 2020-01-16 00:00:00
notAfter: 2020-04-15 12:00:00
How to convert this into df
tried this and got partially successfull:
f = open("out.txt", "r")
a=(f.read())
a=(pd.read_csv(StringIO(data),
header=None,
#use a delimiter not present in the text file
#forces pandas to read data into one column
sep="/",
names=['string'])
#limit number of splits to 1
.string.str.split(':',n=1,expand=True)
.rename({0:'Name',1:'temp'},axis=1)
.assign(temp = lambda x: np.where(x.Name.str.strip()
#look for string that ends
#with a bracket
.str.match(r'(.*[)]$)'),
x.Name,
x.temp),
Name = lambda x: x.Name.str.replace(r'(.*[)]$)','Name')
)
#remove whitespace
.assign(Name = lambda x: x.Name.str.strip())
.pivot(columns='Name',values='temp')
.ffill()
.dropna(how='any')
.reset_index(drop=True)
.rename_axis(None,axis=1)
.filter(['Name','commonName','issuer','notBefore','notAfter'])
)
But this is looping and giving me multiple data like single rows has a multiple duplicates
Serge Ballesta :
The file is not is csv format, so you should not read it with read_csv
but parse it by hand. Here you could do:
with open("out.txt") as fd:
cols = {'commonName','issuer','notBefore','notAfter'} # columns to keep
rows = [] # list of records
for line in fd:
line = line.strip()
if ':' in line:
elt = line.split(':', 1) # data line: parse it
if elt[0] in cols:
rec[elt[0]] = elt[1]
elif len(line) > 0:
rec = {'Name': line} # initial line of a block
rows.append(rec)
a = pd.DataFrame(rows) # and build the dataframe from the list of records
It gives:
Name commonName issuer notAfter notBefore
0 google.com('172.217.163.46', 443) *.google.com GTS CA 1O1 2020-05-06 11:47:11 2020-02-12 11:47:11
1 facebook.com('31.13.79.35', 443) *.facebook.com DigiCert SHA2 High Assurance Server CA 2020-04-15 12:00:00 2020-01-16 00:00:00