Need python script to read a file

more data.txt

user1:psw1:Moni,Admi
user2:psw2:Depl,Moni,Admi

I wish to perform the following task on the data.txt in my python script

  1. read line by line data.txt
  2. Once you read the first line split it using delimiter ":" i.e "user1" "psw1" & "Moni,Admi"
  3. split the 3rd string i.e "Moni,Admi" using delimiter ","

same process for lines 2,3...etc

Below is what i tried but it is incorrect:

f = open("data.txt", "r")
    for line in f.readlines():
        etake = line.partition(":")
        print "Hello"
        print(etake[0])
        for entry in etake:
                print entry[0]
                print entry[1]
                words = entry[2].split(,)
                for word in words:
                        print word

I tried to change etake = line.partition(":") with etake = line.split(":") but it still fails.

Can someone please help ?

I am guessing here, using Python 2.7.x...
Is this what you are after?

f=open("/Users/barrywalker/data.txt", "r")
char=""
for line in f.readlines():
	strng=line.replace(":", " ")
	strng=strng.replace(",", " ")
	char=char+strng
f.close()
print char

Results, MBP 13 inch, OSX 10.7.5, default bash terminal:-

Last login: Wed Sep 21 18:05:57 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Python
AMIGA:barrywalker~/Desktop/Code/Python> python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> execfile("str_test.py")
user1 psw1 Moni Admi
user2 psw2 Depl Moni Admi

>>> _
1 Like

Thx for the suggestion but, I want to store the elements e.g: [user1 psw1 Moni Admi] in "DIFFERENT" variables.

username=user1
password=psw1
groups=Moni,Admi

this variable values will change for the second line read from the "data.txt" file like:

username=user2
password=psw2
groups=Depl,Moni,Admi

Well the reqirements here are nothing like your OP!
I noticed that you edited your post what was the edit???
Try this; it CAN be slimmed down. I will let you do that...

# python 2.7.x
f=open("/Users/barrywalker/data.txt", "r")
for line in f.readlines():
	lst=line.split(":")
	username=lst[0]
	password=lst[1]
	groups=lst[2]
	print "Username=%s" %(username)
	print "Password=%s" %(password)
	print "Groups=%s" %(groups)
f.close()

Results:-

Last login: Wed Sep 21 20:14:55 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Python
AMIGA:barrywalker~/Desktop/Code/Python> python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> execfile("lst_test.py")
Username=user1
Password=psw1
Groups=Moni,Admi

Username=user2
Password=psw2
Groups=Depl,Moni,Admi

>>> exit()
AMIGA:barrywalker~/Desktop/Code/Python> _

IMPORTANT NOTE:-
you will be re-using the same variables per line loop!
You will have to intercept each loop to do something with the variables, that is up to you.

EDIT:-
Cure copy and paste in code.

1 Like

You helped resolve my issue!! You taught me something . Thank you :):b: