Python Forum

Full Version: Print line
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
How can i use a printed line as a varible?
I mean "a printed line (adsialdh) = x"
What do you mean exactly?
s = "Printed line :)"
print(s)
?
I am using a for loop to print a txt which contains emails
I want all those printed emails to be looped as well and define x.
x = printed email

Example:

[email protected] #1 email
[email protected] #2 email
.
.
.
I can t express myself very good.
Use a dictionary,you loop over with enumerate(set start at 1).
data ='''\
[email protected]
[email protected]''''

>>> d = {}
>>> for index,email in enumerate(data.split('\n'), 1):
...     d['email{}'.format(index)] = email
...     
>>> d
{'email1': '[email protected]',
'email2': '[email protected]'}

>>> d['email2']
'[email protected]'
A short representation of the file and the code snippet with what you tried so far will clear the picture.
You want 'x' to contain one email per loop or you want to add any printed email to 'x' as a list?
I want "x" to contain 1 email per loop. :>
How does the file is formatted?
It's a txt. like

[email protected]
[email protected]
.
.
.
etc

To be more precise , i need something like  : first line to be used in a send email script, run it and go at sencond line . etc. looping like that until the emails from txt file it ends. Sorry for bad english.
with open('emails.txt', 'r') as inputfile:
    while inputfile:
        email = inputfile.readline()
        print(email)
        # do something else
Hmm or.
with open('emails.txt', 'r') as f_obj:
   for line in f_obj
       print(line)
so here get email variable overwritten in every loop iteration email = inputfile.readline()
What we have at end is just the last line stored in email variable.
Maybe easier to see if i do the same in my code:
with open('emails.txt', 'r') as f_obj:
   for line in f_obj:
       email = line
       print(email)
Pages: 1 2