-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.

2
  • 1
    Possible duplicate of How to read a file line-by-line into a list? Commented Feb 10, 2017 at 18:46
  • Actually, in retrospect, "Too Broad" is probably a better close reason, as this question asks how to do multiple things. Commented Feb 10, 2017 at 18:51

3 Answers 3

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 
Sign up to request clarification or add additional context in comments.

Comments

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 [email protected] line3 1-800-smth-here sensorname link file = open(“testfile.txt”, “r”) client_email = file.readline(1) print client_email 

would result in

[email protected] 

1 Comment

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)
-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.

2 Comments

It's inappropriate to post an answer that tells a user to look at another question. Please flag such questions as duplicates.
I thought it was a borderline-duplicate as he added a little new aspect of assigning it to a variable, but you are right.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.