-3

I have .txt file that has 6 lines on it.

Line 1 name 
Line 2 eamil address 
line 4 phone number 
line 5 sensor name 
line 6 link .

I want to read those 6 lines in python and forward an email to the email address listed in the second line. I have a script that does this . But I don't know how to do this from .txt file . Thanks.

DYZ
  • 55,249
  • 10
  • 64
  • 93
  • 1
    Possible duplicate of [How to read a file line-by-line into a list?](http://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list) – jpmc26 Feb 10 '17 at 18:46
  • Actually, in retrospect, "Too Broad" is probably a better close reason, as this question asks how to do multiple things. – jpmc26 Feb 10 '17 at 18:51

3 Answers3

0
with open("filename", "r") as f:
    for l in f:
      // do your processing, maybe keep track of how many lines you see since you need to do something different on each line
ferahgo
  • 408
  • 1
  • 4
  • 11
0

You said email lies in the second line of the file?

you can manipulate txt files by line using the readline() function.

usage example:

text file:

John Smith
john@smith.tld
line3
1-800-smth-here
sensorname
link

file = open(“testfile.txt”, “r”) 
client_email = file.readline(1)
print client_email

would result in

john@smith.tld
  • Thanks , but it only reads the first two letters from the first line not the second line when I put :client_email = file.readline(2) – Ahmed Albadwi Feb 10 '17 at 22:06
-2

Have a look at this question: How do I read a text file into a string variable in Python

It shows you how to read a file line per line into an array.

So you can do:

with open('data.txt', 'r') as myfile:
     data=myfile.read().replace('\n', '')

data[1] would then be the email address.

Please do proper research before asking.

Community
  • 1
  • 1
rudib
  • 227
  • 1
  • 10
  • It's inappropriate to post an answer that tells a user to look at another question. Please flag such questions as duplicates. – jpmc26 Feb 10 '17 at 18:43
  • I thought it was a borderline-duplicate as he added a little new aspect of assigning it to a variable, but you are right. – rudib Feb 10 '17 at 18:46