显示特定数量的字符

时间:2019-01-18 13:14:10

标签: python urllib

我正在尝试解决该程序

”使用urllib复制

的先前练习

(1)从URL检索文档 (2)最多显示3000个字符,并且 (3)计算文档中字符的总数。不要

担心此练习的标题,只需显示前3000个

文档内容的字符。”

这就是我想出的,这给了我结果,但是我想知道是否有一种方法可以不使用列表

import urllib.request, urllib.parse, urllib.error


user_url = input("Enter a link: ")
if len(user_url) < 1 : user_url = 'http://data.pr4e.org/romeo-full.txt'
try :
fhand = urllib.request.urlopen(user_url)
except :
    print("Enter a proper URL", user_url)
    quit()

lst = list()
count = 0
for line in fhand :
    words = line.decode().split()
    for word in words :
        #print(word)
        for char in word :
            count = count + 1
            lst.append(char)
print(lst[:3001])
print(count)

4 个答案:

答案 0 :(得分:0)

您可以这样做:

fhand = urllib.request.urlopen(user_url)
result = fhand.read(3000) # read 3000 BYTES (since it's not specified what a 'character' is)

或者阅读所有内容,解码并输出3000个字符:

result = fhand.read().decode()[:3000] # note that whitespace is a character too

答案 1 :(得分:0)

一种方法,如果您需要避免字符中的空格并仅保留3000个字符。

char_count = 30
curr_char_count = 0
complete_str = ""
for line in fhand :
    new_line = line.decode().replace(" ", "")
    if len(complete_str) + len(new_line) <= char_count:
        complete_str = complete_str + new_line
    else:
        complete_str = complete_str + new_line[:((len(complete_str)+len(new_line)) - char_count)]
        break

print complete_str

答案 2 :(得分:0)

没有可接受的答案,为什么? “(2)最多显示3000个字符”有两个答案,如果要获取文件“(3)计算文档中字符总数的长度”,则有两个答案。 (这至少与txt文档一起使用),您可以在下面使用代码。

import urllib.request, urllib.parse, urllib.error

user_url = 'http://data.pr4e.org/romeo-full.txt'
fhand = urllib.request.urlopen(user_url)
result = fhand.read() 
print(len(result))

这是对ForceBru的代码进行的稍微修改。

答案 3 :(得分:0)

因为我们只需要在<= 3000的情况下打印完整文件 我们可以指定要读取和打印的长度(以字节为单位)。

import urllib.request, urllib.parse, urllib.error

url = input("Type the full url you want to connect: ")
fhand = urllib.request.urlopen(url)
content = fhand.read()

print(content[:3001].decode().strip()) 

print("\nDocument length is {}".format(len(content)))