Python读取文件缓冲区实例时出错

2024-06-01 06:16:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试着从强力球获胜号码文件中读出: http://www.powerball.com/powerball/winnums-text.txt

我试着一行一行地得到它,我有这样的代码:

import urllib.request
with urllib.request.urlopen("http://www.powerball.com/powerball/winnums-text.txt") as file:
    next(file)
    for line in file:
        line.lstrip("b'")
        line.rstrip(" \r\n'")
        print(line)

文件中的每一行都这样打印出来:

b'12/06/1997  15  26  28  08  43  36  \r\n'
b'12/03/1997  18  09  14  47  42  32  \r\n'
b'11/29/1997  11  27  13  02  31  23  \r\n'
b'11/26/1997  15  46  34  23  40  35  \r\n'
b'11/22/1997  22  31  03  07  14  02  \r\n'

我得到一个错误:

File "powerball.py", line 5, in <module>
    line.lstrip("b'")
TypeError: 'str' does not support the buffer interface

我试图摆脱多余的字符,并使行如下:

12/06/1997  15  26  28  08  43  36

我该怎么解决这个问题?你知道吗


Tags: 文件textintxtcomhttprequestwww
1条回答
网友
1楼 · 发布于 2024-06-01 06:16:17

line是字节序列,而不是字符串。使用str函数将其转换为字符串。你知道吗

import urllib.request
with urllib.request.urlopen("http://www.powerball.com/powerball/winnums-text.txt") as file:
    next(file)
    for bline in file:
        line = str(bline, "utf-8")
        print(line)
网友
2楼 · 发布于 2024-06-01 06:16:17

正如有人已经提到的,文件是以二进制模式读取的。您需要将字符串转换为文本编码格式。 您可以通过以下方法解决此问题:

line = line.decode("utf-8","ignore")

这应该给你你所期望的行为。你知道吗

网友
3楼 · 发布于 2024-06-01 06:16:17

我强烈建议对这种IO使用pandas,它将在一行代码中处理http请求、解析和所有内容;另外,您还可以使用它进行数据分析:

import pandas as pd
df = pd.read_csv('http://www.powerball.com/powerball/winnums-text.txt')
print(df)

     Draw Date   WB1 WB2 WB3 WB4 WB5 PB  PP
0     02/24/2016  67  21  65  31  64  05  3
1     02/20/2016  54  12  11  16  15  25  5
2     02/17/2016  29  27  07  40  17  25  2
3     02/13/2016  07  15  36  18  19  20  2
4     02/10/2016  02  62  40  50  03  05  2
5     02/06/2016  13  04  36  31  52  08  3
6     02/03/2016  26  60  67  31  28  23  3
7     01/30/2016  16  05  12  31  43  18  4
8     01/27/2016  40  52  03  67  12  21  2
9     01/23/2016  32  22  40  69  34  19  4
10    01/20/2016  44  05  39  69  47  24  5
11    01/16/2016  61  52  51  64  03  06  2

相关问题 更多 >