I have a .txt file that looks like:
'192.168.0.1','sarah','privatepassword'
'192.168.20.2','john','password1'
How can I take the lines of the file and assign them to variables for ip, username, and password? I can change the format of the .txt file if needed.
CodePudding user response:
Try the code below.
Starting by opening the .txt file, and make a loop through each line. Then, you can use the variables ip, name and pwd to hold elements in each line of the file.
f = open(r'test.txt' , 'r')
for x in f:
x=x.rstrip()
x = x.split(',')
ip,name, pwd = x
print(ip)
print(name)
print(pwd)
CodePudding user response:
all_creds = []
with open("separate.txt", "r") as creds:
creds = myfile.readline()
while creds:
ip, un, pw = creds.split(',')
all_creds.append((ip, un, pw))
creds = myfile.readline()
print(all_creds)
CodePudding user response:
You can use split()
and store the result in a list
a = []
s = "'sarah','192', 'pass'"
a.append(s.split(","))
a
is now a list containing a list of three elements.
[["'sarah'", "'192'", " 'pass'"]]