0

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.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
David
  • 3
  • 2
  • Welcome to Stack Overflow. The question asked here is functionally "how do I interpret the contents of a file?", which is beyond the scope of a Stack Overflow question. You should try to follow a tutorial, or formal programming instruction. Since you are in control of the data format, there are various approaches available to you. The format you show here looks similar to CSV, which has built-in support in the standard library. You should try using a search engine to look that up. – Karl Knechtel Mar 16 '22 at 04:28
  • Anyway, please read [ask] and make sure you understand that this is *not a discussion forum*. Comments like "any help or advice is appreciated!" are not appropriate for questions here, because they do not help to clarify *the question*. Questions asked here are primarily for the benefit of the potentially large number of people who could find them later with a search engine - not simply for the one person asking. Similarly, your level of experience does not help us answer the question, does not change the correct answer, and does not help future viewers understand the answer. – Karl Knechtel Mar 16 '22 at 04:29
  • I edited the question, therefore, to include only the parts that are relevant. You should help now by re-editing the question, to add detail and show *what result you expect* for this input. – Karl Knechtel Mar 16 '22 at 04:31
  • From the comments you've left, it seems like the main problem you are asking about is really "how do I assign values from a tuple to separate variables?" You should probably try to follow a Python tutorial to learn such fundamentals, but we do have existing questions to address that - e.g., https://stackoverflow.com/questions/18372952/split-tuple-items-to-separate-variables. – Karl Knechtel Mar 16 '22 at 04:36

3 Answers3

0

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'"]]
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • Thank you! How would I assign these elements to different variables in my main code? – David Mar 16 '22 at 03:58
  • Well you don't because if the file has 1,000 lines you would need 3,000 variables, and you don't know how many are you going to need, hence you store them in an array (or process them and discard them as in your approved answer) – OscarRyz Mar 16 '22 at 08:02
0
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)
kr1tzy
  • 138
  • 2
  • 14
  • Thanks for your response! When I run this, it treats the creds.split result as one value so I am unable to assign it to three different variables. Do you have any recommendations? – David Mar 16 '22 at 04:31
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Mar 17 '22 at 01:05
0

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)
vanBran
  • 16
  • 1
  • 2