473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Python 3.7 filter text file lines and set results into an excel cell

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:

Expand|Select|Wrap|Line Numbers
  1. result = []
  2. with open("text.txt") as origin_file:
  3.     for line in origin_file:
  4.         if 'username' in line:
  5.             result.append(line.split(' ')[2])
  6.             #result.append(int(line))
  7.     #print(result)
  8.     #print(len(result))
  9.     # Display all string elements in list.
  10.     for st in result:
  11.         print(st)
  12.  
  13. result2 = []
  14. with open("text.txt") as origin_file:
  15.     for line in origin_file:
  16.         if 'username' in line:
  17.             result2.append(line.split(' ')[5])
  18.             #result.append(int(line))
  19.     #print(result)
  20.     #print(len(result))
  21.     # Display all string elements in list.
  22.     for st2 in result2:
  23.         print(st2)
  24.  
  25. result3 = []
  26. with open("text.txt") as origin_file:
  27.     for line in origin_file:
  28.         if 'username' in line:
  29.             result3.append(line.split(' ')[7])
  30.             #result.append(int(line))
  31.     #print(result)
  32.     #print(len(result))
  33.     # Display all string elements in list.
  34.     for st3 in result3:
  35.         print(st3)
  36. import xlwt
  37.  
  38.  
  39. workbook = xlwt.Workbook()
  40. worksheet = workbook.add_sheet('Test')
  41.  
  42. style_string = "font: bold on"
  43. style = xlwt.easyxf(style_string)
  44.  
  45. worksheet.write(0, 0, 'IP Address', style=style)
  46. worksheet.write(0, 1, 'Hostname', style=style)
  47. worksheet.write(0, 2, 'Users found', style=style)
  48.  
  49. worksheet.write(1, 0, st)
  50. worksheet.write(1, 1, st2)
  51. worksheet.write(1, 2, st3)
  52. worksheet.write(2, 0, st)
  53. worksheet.write(2, 1, st2)
  54. worksheet.write(2, 2, st3)
  55. worksheet.write(3, 0, st)
  56. worksheet.write(3, 1, st2)
  57. worksheet.write(3, 2, st3)
  58. worksheet.write(4, 0, st)
  59. worksheet.write(4, 1, st2)
  60. worksheet.write(4, 2, st3)
  61.  
  62. workbook.save('test.xls')
Jun 6 '18 #1
0 937

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Ruben | last post by:
Hello. I am trying to read a small text file using the readline statement. I can only read the first 2 records from the file. It stops at the blank lines or at lines with only spaces. I have a...
4
by: Stuk | last post by:
Hi, im false beginner in C so that`s why im writting here :). I have to write a Text Reformater, which should read data from text file every Verse. In text file may appear special directives (for...
1
by: mail2atulmehta | last post by:
Hi, I do not know if this is right place for this, but i need some help. I have a text file, whose values are seprated by a delimiter. I want to open this file in excel, ( not import it) . I have...
4
by: Nick | last post by:
Hi, Any sample code which test the encoding of a text file? Most results I found in google is : unless you read the file, you will never know the encoding, but the problem is I need to tell...
3
by: miben | last post by:
I have a file that is of arround 250,000 records long. Currently, I do a simple forward only search on a record column to find the record and return the record if found. If the record is at the...
2
by: ownowl | last post by:
Hello beginer under python, I have a problem to get lines in a text file. lines have inside the \n (x0A) char, and le readline method split the line at this char too (not only at x0Dx0A). for...
3
by: Daniel Nogradi | last post by:
Hi list, I have an awk program that parses a text file which I would like to rewrite in python. The text file has multi-line records separated by empty lines and each single-line field has two...
9
JavaStudent07
by: JavaStudent07 | last post by:
I am importing a data file to a sorting method and need to know how to take each line of the imported data and set it equal to an array. EX. 1.) 2 3 4 5 2.) 3 6 2 6 3.) 9 2 5 6 i want...
1
by: Quentin | last post by:
I want to take a comma delimited text file and export it to excel format, using the comma as the delimiter. Any help on this would be great, thank you.
4
by: CoachBarker | last post by:
I hope I can explain this. VB.net Visual Studio 2005 What I need to do is take the names in a text file and read them into my application. That I have done and put them in an array but I don't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.