1. 程式人生 > >青魔法Python 讀取txt文件

青魔法Python 讀取txt文件

青魔法Python 讀取txt文件

本文作者:魏泯

我的部落格源地址:https://www.cnblogs.com/Asterism-2012/

做有效率的魔法師

*跳轉到文章結尾*

我自己貼上的代理IP儲存到.txt文字文件中,準備用於測試使用。

由於讀取文字行的最佳方式是利用迭代器來對檔案進行逐行讀取,所以使用:

In [2]: f = open(r'E:\PyAsterism\Web-Crawler\bokeFlush\proxyUrl.txt')

但是在進行檔案操作的時候出現了編碼問題:

In [3]: lines = f.readlines()
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-3-d1e99c5e7da7> in <module>()
----> 1 lines = f.readlines()

UnicodeDecodeError: 'gbk' codec can't decode byte 0xa1 in position 80: illegal multibyte sequence

判斷異常資訊是編碼問題,可以更改一種讀取方法(二進位制讀取rb):

f = open(r'E:\PyAsterism\Web-Crawler\bokeFlush\proxyUrl.txt','rb')
lines = f.readlines()
lines

結果就變成了這樣:

[b"['113.16.245.32', '8118', '\xe9\xab\x98\xe5\x8c\xbf\xe4\xbb\xa3\xe7\x90\x86IP', 'HTTP', '\xe9\xab\x98\xe5\x8c\xbf_\xe5\xb9\xbf\xe8\xa5\xbf\xe5\x8d\x97\xe5\xae\x81\xe5\xb8\x82\xe7\x94\xb5\xe4\xbf\xa1', '3\xe7\xa7\x92', '2019/1/7 15:18:13']\r\n", b"['125.107.41.116', '8060', '\xe9\xab\x98\xe5\x8c\xbf\xe4\xbb\xa3\xe7\x90\x86IP', 'HTTP', '\xe9\xab\x98\xe5\x8c\xbf_\xe6\xb5\x99\xe6\xb1\x9f\xe7\x9c\x81\xe7\xbb\x8d\xe5\x85\xb4\xe5\xb8\x82\xe7\x94\xb5\xe4\xbf\xa1', '1\xe7\xa7\x92', '2019/1/7 14:47:20']\r\n", b"['125.107.125.10', '8060', '\xe9\xab\x98\xe5\x8c\xbf\xe4\xbb\xa3\xe7\x90\x86IP', 'HTTP', '\xe9\xab\x98\xe5\x8c\xbf_\xe6\xb5\x99\xe6\xb1\x9f\xe7\x9c\x81\xe7\xbb\x8d\xe5\x85\xb4\xe5\xb8\x82\xe7\x94\xb5\xe4\xbf\xa1', '1\xe7\xa7\x92', '2019/1/7 14:16:11']\r\n", b"['115.206.143.198', '8060', '\xe9\xab\x98\xe5\x8c\xbf\xe4\xbb\xa3\xe7\x90\x86IP', 'HTTP', '\xe9\xab\x98\xe5\x8c\xbf_\xe6\xb5\x99\xe6\xb1\x9f\xe7\x9c\x81\xe6\x9d\xad\xe5\xb7\x9e\xe5\xb8\x82\xe7\x94\xb5\xe4\xbf\xa1', '6\xe7\xa7\x92', '2019/1/7 13:45:09']\r\n", b"['111.165.110.149', '8060', '\xe9\xab\x98\xe5\x8c\xbf\xe4\xbb\xa3\xe7\x90\x86IP', 'HTTP', '\xe9\xab\x98\xe5\x8c\xbf_\xe5\xa4\xa9\xe6\xb4\xa5\xe5\xb8\x82\xe8\x81\x94\xe9\x80\x9a', '0\xe7\xa7\x92', '2019/1/7 13:45:07']\r\n", b"['171.42.3.251', '8060', '\xe9\xab\x98\xe5\x8c\xbf\xe4\xbb\xa3\xe7\x90\x86IP', 'HTTP', '\xe9\xab\x98\xe5\x8c\xbf_\xe6\xb9\x96\xe5\x8c\x97\xe7\x9c\x81\xe7\x94\xb5\xe4\xbf\xa1', '6\xe7\xa7\x92', '2019/1/7 13:14:29']\r\n", b"['123.115.128.179', '8060', '\xe9\xab\x98\xe5\x8c\xbf\xe4\xbb\xa3\xe7\x90\x86IP', 'HTTP', '\xe9\xab\x98\xe5\x8c\xbf_\xe5\x8c\x97\xe4\xba\xac\xe5\xb8\x82\xe8\x81\x94\xe9\x80\x9a', '2\xe7\xa7\x92', '2019/1/7 12:43:20']\r\n", b"['180.175.167.186', '8060', '\xe9\xab\x98\xe5\x8c\xbf\xe4\xbb\xa3\xe7\x90\x86IP', 'HTTP', '\xe9\xab\x98\xe5\x8c\xbf_\xe4\xb8\x8a\xe6\xb5\xb7\xe5\xb8\x82\xe7\x94\xb5\xe4\xbf\xa1', '0\xe7\xa7\x92', '2019/1/7 12:12:21']\r\n", b"['180.175.140.194', '8060', '\xe9\xab\x98\xe5\x8c\xbf\xe4\xbb\xa3\xe7\x90\x86IP', 'HTTP', '\xe9\xab\x98\xe5\x8c\xbf_\xe4\xb8\x8a\xe6\xb5\xb7\xe5\xb8\x82\xe7\x94\xb5\xe4\xbf\xa1', '1\xe7\xa7\x92', '2019/1/7 11:41:04']\r\n", b"['110.52.235.195', '9999', '\xe9\xab\x98\xe5\x8c\xbf\xe4\xbb\xa3\xe7\x90\x86IP', 'HTTP', '\xe9\xab\x98\xe5\x8c\xbf_\xe6\xb9\x96\xe5\x8d\x97\xe7\x9c\x81\xe5\xb2\xb3\xe9\x98\xb3\xe5\xb8\x82\xe8\x81\x94\xe9\x80\x9a', '0\xe7\xa7\x92', '2019/1/7 11:41:04']\r\n", b"['123.207.247.86', '9999', '\xe9\xab\x98\xe5\x8c\xbf\xe4\xbb\xa3\xe7\x90\x86IP', 'HTTP', '\xe9\xab\x98\xe5\x8c\xbf_\xe5\xb9\xbf\xe4\xb8\x9c\xe7\x9c\x81\xe5\xb9\xbf\xe5\xb7\x9e\xe5\xb8\x82\xe8\x85\xbe\xe8\xae\xaf\xe4\xba\x91', '1\xe7\xa7\x92', '2019/1/7 8:04:11']\r\n", b"['111.230.113.238', '9999', '\xe9\xab\x98\xe5\x8c\xbf\xe4\xbb\xa3\xe7\x90\x86IP', 'HTTP', '\xe9\xab\x98\xe5\x8c\xbf_\xe5\xb9\xbf\xe4\xb8\x9c\xe7\x9c\x81\xe5\xb9\xbf\xe5\xb7\x9e\xe5\xb8\x82\xe8\x85\xbe\xe8\xae\xaf\xe4\xba\x91', '5\xe7\xa7\x92', '2019/1/7 7:33:34']\r\n", b"['119.190.193.176', '8060', '\xe9\xab\x98\xe5\x8c\xbf\xe4\xbb\xa3\xe7\x90\x86IP', 'HTTP', '\xe9\xab\x98\xe5\x8c\xbf_\xe5\xb1\xb1\xe4\xb8\x9c\xe7\x9c\x81\xe8\x81\x94\xe9\x80\x9a', '3\xe7\xa7\x92', '2019/1/7 6:31:02']\r\n", b"['27.208.138.208', '8060', '\xe9\xab\x98\xe5\x8c\xbf\xe4\xbb\xa3\xe7\x90\x86IP', 'HTTP', '\xe9\xab\x98\xe5\x8c\xbf_\xe5\xb1\xb1\xe4\xb8\x9c\xe7\x9c\x81\xe5\xa8\x81\xe6\xb5\xb7\xe5\xb8\x82\xe8\x81\x94\xe9\x80\x9a', '0\xe7\xa7\x92', '2019/1/7 6:00:08']\r\n", b"['144.255.13.138', '9999', '\xe9\xab\x98\xe5\x8c\xbf\xe4\xbb\xa3\xe7\x90\x86IP', 'HTTPS', 'SSL\xe9\xab\x98\xe5\x8c\xbf_\xe5\xb1\xb1\xe4\xb8\x9c\xe7\x9c\x81\xe7\x94\xb5\xe4\xbf\xa1', '4\xe7\xa7\x92', '2019/1/7 5:29:10']\r\n"]

但是這樣也是可以解決的,先說一下思路:檔案為我們打印出來是列表的形式,我們可以將這些內容從列表中取出來然後進行轉碼。
程式碼:

def EncodeStr(str):     # 定義一個解碼的函式,接收的引數型別為str
    return str.decode()

x = map(EncodeStr, lines)       # 使用高階函式map進行對映
for info in x:      # 此時x是一個可迭代物件
     print(info)

成功!列印結果:

['113.16.245.32', '8118', '高匿代理IP', 'HTTP', '高匿_廣西南寧市電信', '3秒', '2019/1/7 15:18:13']

['125.107.41.116', '8060', '高匿代理IP', 'HTTP', '高匿_浙江省紹興市電信', '1秒', '2019/1/7 14:47:20']

['125.107.125.10', '8060', '高匿代理IP', 'HTTP', '高匿_浙江省紹興市電信', '1秒', '2019/1/7 14:16:11']

['115.206.143.198', '8060', '高匿代理IP', 'HTTP', '高匿_浙江省杭州市電信', '6秒', '2019/1/7 13:45:09']

['111.165.110.149', '8060', '高匿代理IP', 'HTTP', '高匿_天津市聯通', '0秒', '2019/1/7 13:45:07']

['171.42.3.251', '8060', '高匿代理IP', 'HTTP', '高匿_湖北省電信', '6秒', '2019/1/7 13:14:29']

['123.115.128.179', '8060', '高匿代理IP', 'HTTP', '高匿_北京市聯通', '2秒', '2019/1/7 12:43:20']

['180.175.167.186', '8060', '高匿代理IP', 'HTTP', '高匿_上海市電信', '0秒', '2019/1/7 12:12:21']

['180.175.140.194', '8060', '高匿代理IP', 'HTTP', '高匿_上海市電信', '1秒', '2019/1/7 11:41:04']

['110.52.235.195', '9999', '高匿代理IP', 'HTTP', '高匿_湖南省岳陽市聯通', '0秒', '2019/1/7 11:41:04']

['123.207.247.86', '9999', '高匿代理IP', 'HTTP', '高匿_廣東省廣州市騰訊雲', '1秒', '2019/1/7 8:04:11']

['111.230.113.238', '9999', '高匿代理IP', 'HTTP', '高匿_廣東省廣州市騰訊雲', '5秒', '2019/1/7 7:33:34']

['119.190.193.176', '8060', '高匿代理IP', 'HTTP', '高匿_山東省聯通', '3秒', '2019/1/7 6:31:02']

['27.208.138.208', '8060', '高匿代理IP', 'HTTP', '高匿_山東省威海市聯通', '0秒', '2019/1/7 6:00:08']

['144.255.13.138', '9999', '高匿代理IP', 'HTTPS', 'SSL高匿_山東省電信', '4秒', '2019/1/7 5:29:10']

*回到文章目錄再看看*</a