I would like to retrieve a filtered data from a text file to excel. Right now I am able to retrive just the last found line in the text file
this is my text file:
I am Fred username is fred from USA cd
I am Robert username is bob from USA cd
I am John username is john from China cd
I am Frank username is frank from France cd
I am getting 3 lines with the same name, username, from (Frank, frank, france)
Also, the prints in the code gives me the results:
Fred
Robert
John
Frank
fred
bob
john
frank
USA
USA
China
France
I want to set in the excel file 3 collumns Name, Username, From
This is my code:
this is my text file:
I am Fred username is fred from USA cd
I am Robert username is bob from USA cd
I am John username is john from China cd
I am Frank username is frank from France cd
I am getting 3 lines with the same name, username, from (Frank, frank, france)
Also, the prints in the code gives me the results:
Fred
Robert
John
Frank
fred
bob
john
frank
USA
USA
China
France
I want to set in the excel file 3 collumns Name, Username, From
This is my code:
Code:
result = []
with open("text.txt") as origin_file:
for line in origin_file:
if 'username' in line:
result.append(line.split(' ')[2])
#result.append(int(line))
#print(result)
#print(len(result))
# Display all string elements in list.
for st in result:
print(st)
result2 = []
with open("text.txt") as origin_file:
for line in origin_file:
if 'username' in line:
result2.append(line.split(' ')[5])
#result.append(int(line))
#print(result)
#print(len(result))
# Display all string elements in list.
for st2 in result2:
print(st2)
result3 = []
with open("text.txt") as origin_file:
for line in origin_file:
if 'username' in line:
result3.append(line.split(' ')[7])
#result.append(int(line))
#print(result)
#print(len(result))
# Display all string elements in list.
for st3 in result3:
print(st3)
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Test')
style_string = "font: bold on"
style = xlwt.easyxf(style_string)
worksheet.write(0, 0, 'IP Address', style=style)
worksheet.write(0, 1, 'Hostname', style=style)
worksheet.write(0, 2, 'Users found', style=style)
worksheet.write(1, 0, st)
worksheet.write(1, 1, st2)
worksheet.write(1, 2, st3)
worksheet.write(2, 0, st)
worksheet.write(2, 1, st2)
worksheet.write(2, 2, st3)
worksheet.write(3, 0, st)
worksheet.write(3, 1, st2)
worksheet.write(3, 2, st3)
worksheet.write(4, 0, st)
worksheet.write(4, 1, st2)
worksheet.write(4, 2, st3)
workbook.save('test.xls')