【问题标题】:Print lines read from file on same line [duplicate]打印从同一行上的文件读取的行[重复]
【发布时间】:2015-04-15 17:54:48
【问题描述】:

我已经尝试了很多不同的方法来实现这一点,只是我想要这个:

import requests
path1 = 'D:\test\Files\/results.txt'
lines1 = open(path1).readlines()
ctr = 0
for i in lines1:
    try:
        r = requests.get(i)
        if r.status_code != 200:
            ctr += 1
            print(i, " Status Code: ", r.status_code)
except requests.ConnectionError:
    ctr += 1
    print(i, " Failed to connect")
print("Counter", ctr)

这样输出:

URL Status Code: xyz

但我得到的是:

URL
Status Code: xyz

那么,用 Python 打印出同一行中的内容的最佳方法是什么?

【问题讨论】:

  • 相信我,我已经试过了。不工作
  • Python 3.x 还是 Python 2.7.X?
  • '这是 Python 版本 3.4.3'
  • i = 文件中的 URL。
  • 只看i 的内容就会告诉你是什么导致了问题。 . .

标签: python


【解决方案1】:

i 是一行。在打印之前将其剥离以删除任何可能的换行符:

 print(i.rstrip(), " Status Code: ", r.status_code)

【讨论】:

  • @JuniorCompressor 抱歉,我在等待倒计时(10 分钟?)时遇到了其他事情。你去吧。
  • 不要道歉。一切正常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-29
  • 1970-01-01
  • 1970-01-01
  • 2014-12-05
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多