Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print line
#1
How can i use a printed line as a varible?
I mean "a printed line (adsialdh) = x"
Reply
#2
What do you mean exactly?
s = "Printed line :)"
print(s)
?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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.
Reply
#4
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]'
Reply
#5
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?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
I want "x" to contain 1 email per loop. :>
Reply
#7
How does the file is formatted?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
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.
Reply
#9
with open('emails.txt', 'r') as inputfile:
    while inputfile:
        email = inputfile.readline()
        print(email)
        # do something else
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with spliting line in print akbarza 3 384 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  Print the line before the corrent line tester_V 9 1,570 Nov-18-2022, 08:39 AM
Last Post: Gribouillis
  Print to a New Line when Appending File DaveG 0 1,222 Mar-30-2022, 04:14 AM
Last Post: DaveG
  If match not found print last line tester_V 2 2,889 Apr-26-2021, 05:18 AM
Last Post: tester_V
  print a line break in writelines() method leodavinci1990 1 6,462 Oct-12-2020, 06:36 AM
Last Post: DeaD_EyE
  Print characters in a single line rather than one at a time hhydration 1 2,033 Oct-10-2020, 10:00 PM
Last Post: bowlofred
  How to print string multiple times on new line ace19887 7 5,744 Sep-30-2020, 02:53 PM
Last Post: buran
  Pattern Require Last Line Print() why? Harshil 4 2,434 Aug-08-2020, 04:54 PM
Last Post: Harshil
  print only last matched line tester_V 24 6,493 Apr-30-2020, 05:16 AM
Last Post: deanhystad
  item from a line to list however when i print the line instead of words i get letters Sutsro 5 2,962 Apr-22-2020, 02:39 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020