脚本在字符串

时间:2018-06-01 11:52:01

标签: python string python-3.x

脚本在大括号后面添加换行符,同时在用户名后面写两个字符串。我认为这是因为我的源文本文件中有编码添加了中断,但据我所知它没有。我误解了写作是怎么回事吗?我想念的东西很简单。看着这个太久了,需要一双新的眼睛。

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

文件的输出如下所示:

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

1 个答案:

答案 0 :(得分:1)

更改第7行和第8行

for i in user_file:
    users_list.append(i)

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

它应该按预期工作。

这是因为i是来自user_file的一行,它以\n结尾。 i.strip()删除尾随换行符。