Hello

I have a txt file with a pattern as below
=============================================
Computer name: Abc IP address: 10.10.10.10
User name: user Last pin : Aug 06 2009 10:44:35
=============================================
Computer name: Def IP address: 10.10.10.11
User name: user2 Last pin : Aug 07 2009 10:44:35
=============================================

All I require is to read the txt file and create a csv file with the keys (values after ":" and tab) separated by a ";" so the output file should have values :

Abc;10.10.10.10;user;Aug 06 2009 10:44:35
Def;10.10.10.11;user2; Last pin : Aug 07 2009 22:45:22

So far I open the txt file and try to search the word eg: Computer name:, User name: and so on but not able to get any further. Python at the moment is very new to me. I feel like stuck at the moment

Any kind of help would be greatly appreciated

Break the file up into groups by reading the file one line at a time and append to a list. When you hit a "Computer name" record, send the list to a function and process that group of records however you want. Then re-define the list as empty and rinse and repeat. You will have to have an additional statement to send the last group of records to the function, since there will be no "Computer name" record found to do that. If you run into problems, post the code that you have and ask for help.

Thank you for replying.

So far my code looks like :
---------------------------------------------------------
f = open("a.txt", "rb")
temp = []
main = []
r = f.readline()
for r in f:
if "Computer name:" in r:
temp.append(r)
def extract(temp):
----------------------------------------------------------
I am able to match the line and append the temp list (this I have tested using the print command).

Now, I am not sure how should I go about taking the list which look like :
and extract the values(ABC;10.10.10.10) using the function. I don't know what technique shall I use to extract these values.

def extract(list_in):
    if len(list_in):
        print "New Group", "-" *60
        for rec in list_in:
            print rec

##f = open("a.txt", "rb")   ## why are you reading this as binary
f = open("a.txt", "r")
temp = []
##r = f.readline()   ## this skips the first rec
for r in f:
    if "Computer name:" in r:
        extract(temp):
        temp = []
    temp.append(r)
extract(temp)  ## the final group 

Use "(CODE)" instead of "(QUOTE)" for code samples.

Hello Woooee
Thank you so much for the assistance. I was able to extract the information just the way I wanted.
Here is the code which can do just that.

def extract(list_in):
	#print "in the definition line"
	if len(list_in):
		#print "New Group", "-" *60
		for i in exclude:
			if i in list_in:
				list_in.remove(i)
				
f = open("a.txt", "r")
computername = "Computer name:"
temp = []
temp2=[]
main = []
exclude=["Computer","name:","IP","address:","User", "name:","Last","pin:"]
for line in f:
	if "Computer name:" in line:
		temp=line.split()
		extract(temp)
		temp2=temp
		temp =[]
	if "User name:" in line:
		temp=line.split()
		extract(temp)
		temp2.extend(temp)
		main.append(temp2)
		temp =[]
		
extract(temp)
print "FINAL RESULT"
print main
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.