我有一个300万行的.txt文件。文件包含如下所示的数据:

# RSYNC: 0 1 1 0 512 0
#$SOA 5m localhost. hostmaster.localhost. 1906022338 1h 10m 5d 1s
# random_number_ofspaces_before_this text $TTL 60s
#more random information
:127.0.1.2:https://www.spamhaus.org/query/domain/$
test
:127.0.1.2:https://www.spamhaus.org/query/domain/$
.0-0m5tk.com
.0-1-hub.com
.zzzy1129.cn
:127.0.1.4:https://www.spamhaus.org/query/domain/$
.0-il.ml
.005verf-desj.com
.01accesfunds.com

我试图将其解析为:
+--------------------+--------------+-------------+-----------------------------------------------------+
|    domain_name     | period_count | parsed_code |                      raw_code                       |
+--------------------+--------------+-------------+-----------------------------------------------------+
| test               |            0 | 127.0.1.2   |  :127.0.1.2:https://www.spamhaus.org/query/domain/$ |
| .0-0m5tk.com       |            2 | 127.0.1.2   |  :127.0.1.2:https://www.spamhaus.org/query/domain/$ |
| .0-1-hub.com       |            2 | 127.0.1.2   |  :127.0.1.2:https://www.spamhaus.org/query/domain/$ |
| .zzzy1129.cn       |            2 | 127.0.1.2   |  :127.0.1.2:https://www.spamhaus.org/query/domain/$ |
| .0-il.ml           |            2 | 127.0.1.4   |  :127.0.1.4:https://www.spamhaus.org/query/domain/$ |
| .005verf-desj.com  |            2 | 127.0.1.4   |  :127.0.1.4:https://www.spamhaus.org/query/domain/$ |
| .01accesfunds.com  |            2 | 127.0.1.4   |  :127.0.1.4:https://www.spamhaus.org/query/domain/$ |
+--------------------+--------------+-------------+-----------------------------------------------------+

为此,我提出了以下建议:
rows = []
raw_code = None
parsed_code = None
with open('dbl-sr-2019-06-02T23_38_27Z.txt', 'r') as f: # assumes the file name is input.txt
    for line in f:
        line = line.rstrip('\n')
        if line.startswith(':127'):
            raw_code = line
            parsed_code = re.split(":", line)[1]
            continue
        if line.startswith('#'):
            continue
        rows.append((line, parsed_code))
#         rows.append((raw_code))
#         rows.extend((line, parsed_code, raw_code))
#         rows.extend((raw_code))

import pandas as pd
df = pd.DataFrame(rows, columns=['domain_name', "parsed_code" 'raw_spamhaus_return_code'])
print(df)

上面代码块中注释掉的行要么没有产生我想要的输出,要么给出了一个错误。我正在努力构建一个超过2列的Pandas数据框架。我可以得到domain_name和另一列。似乎我无法将代码降下来以正确使用.append.extend函数。有人能提供指导吗?

最佳答案

你的问题可能是缺少逗号。
这:

df = pd.DataFrame(rows, columns=[
    'domain_name', 'parsed_code', 'raw_spamhaus_return_code'])

不同于:
df = pd.DataFrame(rows, columns=[
    'domain_name', "parsed_code" 'raw_spamhaus_return_code'])

因为(请注意缺少的逗号):
"parsed_code" 'raw_spamhaus_return_code'

变成一条线。
测试代码:
import re

data = [x.strip() for x in """
    # RSYNC: 0 1 1 0 512 0
    #$SOA 5m localhost. hostmaster.localhost. 1906022338 1h 10m 5d 1s
    # random_number_ofspaces_before_this text $TTL 60s
    #more random information
    :127.0.1.2:https://www.spamhaus.org/query/domain/$
    test
    :127.0.1.2:https://www.spamhaus.org/query/domain/$
    .0-0m5tk.com
    .0-1-hub.com
    .zzzy1129.cn
    :127.0.1.4:https://www.spamhaus.org/query/domain/$
    .0-il.ml
    .005verf-desj.com
    .01accesfunds.com
""".split('\n')[1:-1]]

rows = []
raw_code = None
parsed_code = None
for line in data:
    line = line.rstrip('\n')
    if line.startswith(':127'):
        raw_code = line
        parsed_code = re.split(":", line)[1]
        continue
    if line.startswith('#'):
        continue
    rows.append((line, line.count('.'), parsed_code, raw_code))

import pandas as pd

df = pd.DataFrame(rows, columns=[
    'domain_name', 'period_count ', 'parsed_code',
    'raw_spamhaus_return_code'])
print(df)

结果:
         domain_name  period_count  parsed_code  \
0               test              0   127.0.1.2
1       .0-0m5tk.com              2   127.0.1.2
2       .0-1-hub.com              2   127.0.1.2
3       .zzzy1129.cn              2   127.0.1.2
4           .0-il.ml              2   127.0.1.4
5  .005verf-desj.com              2   127.0.1.4
6  .01accesfunds.com              2   127.0.1.4

                            raw_spamhaus_return_code
0  :127.0.1.2:https://www.spamhaus.org/query/doma...
1  :127.0.1.2:https://www.spamhaus.org/query/doma...
2  :127.0.1.2:https://www.spamhaus.org/query/doma...
3  :127.0.1.2:https://www.spamhaus.org/query/doma...
4  :127.0.1.4:https://www.spamhaus.org/query/doma...
5  :127.0.1.4:https://www.spamhaus.org/query/doma...
6  :127.0.1.4:https://www.spamhaus.org/query/doma...

关于python - Python:append()和extend(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56981256/

10-16 09:58