如何逐行打印在线txt文件?



我的代码列出如下:

import urllib.request, urllib.parse, urllib.error
headers = {'User-Agent':
'''Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
AppleWebKit/537.36 (KHTML, like Gecko) 
Chrome/79.0.3945.117 Safari/537.36'''}
url = 'https://www.py4e.com/code3/mbox.txt'
req = urllib.request.Request(url, headers=headers)
fname = urllib.request.urlopen(req).read().decode()
for line in fname:
print(line)

输出不是逐行输出,而是字符?如何逐行打印文件?多谢!

尝试对字符串使用"按换行符拆分"。

for line in fname.split('n'):
print(line)

您的read()正在将文本读入字符串。

import urllib.request, urllib.parse, urllib.error
headers = {'User-Agent':
'''Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
AppleWebKit/537.36 (KHTML, like Gecko) 
Chrome/79.0.3945.117 Safari/537.36'''}
url = 'https://www.py4e.com/code3/mbox.txt'
req = urllib.request.Request(url, headers=headers)
fname = urllib.request.urlopen(req)
count = 0
for line in fname:
count += 1
print(count)
print(line.decode())

这会产生:

1
From stephen.marquard@uct.ac.za Sat Jan  5 09:14:16 2008
2
Return-Path: <postmaster@collab.sakaiproject.org>
3
Received: from murder (mail.umich.edu [141.211.14.90])
4
by frankenstein.mail.umich.edu (Cyrus v2.3.8) with LMTPA;
5     
Sat, 05 Jan 2008 09:14:16 -0500
6
X-Sieve: CMU Sieve 2.3
7
Received: from murder ([unix socket])
8
by mail.umich.edu (Cyrus v2.2.12) with LMTPA;
9
Sat, 05 Jan 2008 09:14:16 -0500
10
Received: from holes.mr.itd.umich.edu (holes.mr.itd.umich.edu [141.211.14.79])
...

最新更新