从txt文件打印

时间:2016-05-25 17:59:39

标签: python printing text-files

我有一组用户。我想从txt文件中删除用户然后写

URL GOTO=https://www.url.com/user1
TAG POS=1 TYPE=BUTTON ATTR=TXT:Follow
WAIT SECONDS= 27

对于每个用户,直到用户用完或所有示例都已被替换,我该怎么做?

users.txt

rrralu
rebeccamacavei
corinnaco_
andrew1996_
thisisme_r
zabiburuziga
be_real_00
officiel_14_leo
thefullersgroup

我有什么

print("\\reading users")
text_file = open("users.txt", "r")
print(text_file.read())
text_file.close()

2 个答案:

答案 0 :(得分:2)

您可以使用内置string.Template类的实例。请注意我添加的str.format

{user1}

<强>更新

更简单的方法是使用所有字符串共有的$user1方法。替换字段的语法略有不同(import而不是template = '''\ URL GOTO=https://www.url.com/{user1} TAG POS=1 TYPE=BUTTON ATTR=TXT:Follow WAIT SECONDS= 27''' with open('users.txt') as file: for line in file: print(template.format(user1=line.strip())) ),但它的优势在于您不必使用users.txt任何内容来使用它并且效果很好与所有其他format string选项一起使用。

URL GOTO=https://www.url.com/rrralu
TAG POS=1 TYPE=BUTTON ATTR=TXT:Follow
WAIT SECONDS= 27
URL GOTO=https://www.url.com/rebeccamacavei
TAG POS=1 TYPE=BUTTON ATTR=TXT:Follow
WAIT SECONDS= 27
URL GOTO=https://www.url.com/corinnaco_
TAG POS=1 TYPE=BUTTON ATTR=TXT:Follow
WAIT SECONDS= 27
URL GOTO=https://www.url.com/andrew1996_
TAG POS=1 TYPE=BUTTON ATTR=TXT:Follow
WAIT SECONDS= 27
URL GOTO=https://www.url.com/thisisme_r
TAG POS=1 TYPE=BUTTON ATTR=TXT:Follow
WAIT SECONDS= 27
URL GOTO=https://www.url.com/zabiburuziga
TAG POS=1 TYPE=BUTTON ATTR=TXT:Follow
WAIT SECONDS= 27
URL GOTO=https://www.url.com/be_real_00
TAG POS=1 TYPE=BUTTON ATTR=TXT:Follow
WAIT SECONDS= 27
URL GOTO=https://www.url.com/officiel_14_leo
TAG POS=1 TYPE=BUTTON ATTR=TXT:Follow
WAIT SECONDS= 27
URL GOTO=https://www.url.com/thefullersgroup
TAG POS=1 TYPE=BUTTON ATTR=TXT:Follow
WAIT SECONDS= 27

当使用样本var footerRow = gvProductsList.FooterRow; 文件中的数据运行时,两者都将生成以下输出:

decimal newtotal;
foreach (GridViewRow row in gvProductsList.Rows)
{
   Label lblItemTotal = (Label)row.FindControl("lblItemTotal");
   decimal decPrice = decimal.Parse(lblItemTotal.Text, NumberStyles.Currency); 
   newtotal =+ decPrice;
 }
 var footerRow = gvProductsList.FooterRow;
 Label lblTotal = (footerRow.FindControl("lblTotal")) as Label;
 lblTotal.Text = "$" + Convert.ToString(newtotal);

答案 1 :(得分:0)

你必须遍历整个.txt文件。

这可以通过打开文件并使用方法readlines()

来完成

使用您的代码,您必须更改此行:

print(text_file.read())

print(text_file.readlines())

CODE1:

print("\\reading users")
with open("users.txt", "r") as f:
    lines = f.readlines()

print lines

如果您只想打印第7行的值...您必须print lines[6]

修改

运行Code1后,您将获得一个列表,其中每个名称对应一个项目。然后,您只需在循环浏览列表时创建所需的链接。

lines = ['rrralu','rebeccamacavei','corinnaco_','andrew1996_',
         'thisisme_r','zabiburuziga','be_real_00','officiel_14_leo',
         'thefullersgroup']

for line in lines:
    url = 'URL GOTO=https://www.url.com/%s' % (line)
    print url

输出:

\reading users
URL GOTO=https://www.url.com/rrralu
URL GOTO=https://www.url.com/rebeccamacavei
URL GOTO=https://www.url.com/corinnaco_
URL GOTO=https://www.url.com/andrew1996_
URL GOTO=https://www.url.com/thisisme_r
URL GOTO=https://www.url.com/zabiburuziga
URL GOTO=https://www.url.com/be_real_00
URL GOTO=https://www.url.com/officiel_14_leo
URL GOTO=https://www.url.com/thefullersgroup