Mbah Augustine
Mbah Augustine

Reputation: 1

Parsing Text file input in python

I am having problems reading input from text file. Also I noticed that only the last line is read and parsed.

out1.txt:

thin279
gatefin
64hamp
testme

Code:

import time
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--headless")


with open('out1.txt') as f:
    for line in f:
        #do not forget to strip the trailing new line
        userid = line.rstrip("\n")
        print (userid)
for i in range (len(userid)):
    ucheck = ("https://example.com/?profile=" + userid)
    xucheck = webdriver.Chrome(options=chrome_options)
    xucheck.get(ucheck)
    print (ucheck)

Below is the output showing only the last item in the list

=========== RESTART: C:/Users/Administrator/Desktop/project_3/qyes.py ==========
thin279
gatefin
64hamp
testme
https://example.com/?profile=testme
https://example.com/?profile=testme
https://example.com/?profile=testme
https://example.com/?profile=testme
https://example.com/?profile=testme
https://example.com/?profile=testme
>>> 

So I want to know how to get it to all the userid and not just the last one (testme).

Upvotes: 0

Views: 62

Answers (2)

Mbah Augustine
Mbah Augustine

Reputation: 1

This is the code that worked from @SimonN guide and help.

from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--headless")


with open('out1.txt') as f:
    for line in f:
        #do not forget to strip the trailing new line
        userid = line.rstrip("\n")
        print (userid)
        ucheck = ("https://example.com/?profile=" + userid)
        xucheck = webdriver.Chrome(options=chrome_options)
        xucheck.get(ucheck)
        print (ucheck)

Upvotes: 0

Simon Notley
Simon Notley

Reputation: 2136

The content of the second for loop needs to be inside the first one, or you need to change the first one to store all the userids to a list. Currently you are just looping through all the values in the text file, then starting a new loop which is just using the value left at the end of the original loop.

Upvotes: 1

Related Questions