Grout58
Grout58

Reputation: 1

Script adding line break in middle of string

The script is adding a line break right after the curly braces while writing both strings right after the username. I would think this is because of my source text file has encoding in it adding the breaks but as far as I can tell it does not. Am I misunderstanding how write works? Something simple I'm missing. Been looking at this too long and need a new set of eyes.

users_list = []

users = input("File of User's ")

user_file = open(users  + '.txt', 'r')

for i in user_file:
    users_list.append(i)

sql_file = open('sql.txt', 'w')

sql_file.write("SELECT MACHINE.NAME AS SYSTEM_NAME, SYSTEM_DESCRIPTION, 
MACHINE.IP, MACHINE.MAC, MACHINE.ID as TOPIC_ID FROM MACHINE WHERE 
((MACHINE.USER_NAME = '{}') OR ".format(users_list[0]))

for i in users_list:
    sql_file.write("(MACHINE.USER_NAME = '{}')".format(i))
    sql_file.write(" OR ")

The output of the file looks like this:

SELECT MACHINE.NAME AS SYSTEM_NAME, SYSTEM_DESCRIPTION, MACHINE.IP, MACHINE.MAC, MACHINE.ID as TOPIC_ID FROM MACHINE WHERE ((MACHINE.USER_NAME = 'hnelson
') OR (MACHINE.USER_NAME = 'hnelson
') OR (MACHINE.USER_NAME = 'snery
') OR (MACHINE.USER_NAME = 'jherman

Upvotes: 0

Views: 498

Answers (1)

Rahul Goswami
Rahul Goswami

Reputation: 595

change your line 7 and 8

for i in user_file:
    users_list.append(i)

to

for i in user_file:
    users_list.append(i.strip())

and it should work as expected.

It is because i is a line from user_file and it ends with \n. i.strip() removes the trailing newline.

Upvotes: 1

Related Questions