3

lets say you have a basic txt file with:

USERNAME(var1):PASSWORD(var2):VAR3
USERNAME(var1):PASSWORD(var2):VAR3
... etc...

And I want to be able to read that in my script/code as:

Username = var1
Password = var2
something = var3

And implement that like:

username = var1
password = var2
s.login(var1, var2)

file = open(var3, 'r')

I have been spitting through a lot of code, trying to reverse engineer some things, but it's not quite working out. If someone could shed some light that'd be great.

I need to make these lists myself, so I can use any separator I want (,, ;, : etc).

edit, this is what I've tried so far:

all_lines = []

file = open('test.txt', 'r')


for line in file:
    Username,Password,something = line.split(':')
    print(Username,Password,something)

But it gives me the following error:

  File "test.py", line 8
    print(Username,Password,something)
    ^
IndentationError: unexpected indent
Snowlav
  • 325
  • 1
  • 12
  • 26
  • Have you tried searching? These aren't exactly your problem, but they're probably good starting points to see what kinds of operations you should consider: http://stackoverflow.com/questions/11936967/text-file-parsing-with-python http://stackoverflow.com/questions/17105456/parsing-data-from-text-file http://stackoverflow.com/questions/3277503/python-read-file-line-by-line-into-array – William Price Nov 28 '14 at 03:01
  • according to my last few sentences in the post: "I have been spitting through a lot of code, trying to reverse engineer some things, but it's not quite working out. If someone could shed some light that'd be great." I've definitely searched for info and starting points, but failed to create something working. I'll add what I've tried so far. – Snowlav Nov 28 '14 at 03:49
  • Thank you for adding that information to your question; it's important. I did read your last few sentences, but without knowing _what_ you've been looking at or _what_ your best attempt looks like, there's nothing on which to "shed some light". For example, there's no other way I could know you were getting an `IndentationError`. – William Price Nov 28 '14 at 03:58
  • Thank you for the clarification, I will be more in detail in my next questions and make sure to add what I've already tried and/or looked at. – Snowlav Nov 28 '14 at 04:04

2 Answers2

2
with open('basic.txt') as f:
    for line in f:
        username, password, filename = line.split(':')
        print(username, password, filename)
poke
  • 369,085
  • 72
  • 557
  • 602
  • This looks really neat, and works for one line, but as I commented above: I've tried this but it's not acting how I want. I have multiple lines. Each line has different info needed. for example: each line has a username and password, I want to be able to login on each account for each line in the file. – Snowlav Nov 28 '14 at 03:47
  • If you try it out, you can see that it will print for each line. So you just need to do your login stuff in the loop to make it work for every login combination in your file. – poke Nov 28 '14 at 03:49
  • correct, I made a spelling mistake. It prints it as the following: ('usn2', 'pass2', 'List2.txt\n') the \n and '', symbols will cause problems in logging in, or won't they? – Snowlav Nov 28 '14 at 03:53
  • @Snowlav You can use `filename.strip()` to remove leading and trailing whitespace (the `\n`). The quotes are not part of the variable’s values but are caused from the `print` statement, so don’t worry about those. – poke Nov 28 '14 at 11:35
0

If all lines have the same format, you can split a line in that file into your variables as follows:

a_line = "USERNAME(var1):PASSWORD(var2):VAR3"
Username,Password,something = a_line.split(':')

print(Username,Password,something)

The result is:

USERNAME(var1) PASSWORD(var2) VAR3

Or in more general way:

all_lines = []

with open('infile.txt','r') as fin:
    all_lines = fin.readlines()


for a_line in all_lines:
    Username,Password,something = a_line.split(':')
    print(Username,Password,something)
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • I've tried this but it's not acting how I want. I have multiple lines. Each line has different info needed. for example: each line has a username and password, I want to be able to login on each account for each line in the file. – Snowlav Nov 28 '14 at 03:46