gpt4 book ai didi

python - 需要解析的异常表

转载 作者:行者123 更新时间:2023-11-30 22:57:04 24 4
gpt4 key购买 nike

如何解析这种类型的表?

https://primes.utm.edu/lists/small/10000.txt

                     The First 10,000 Primes
(the 10,000th is 104,729)
For more information on primes see http://primes.utm.edu/

2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97 101 103 107 109 113

这些不是逗号分隔的数字或 xml 结构的数字。您知道有什么方法可以将它们读入列表吗?

最佳答案

只需知道数据从第四行开始到最后一行结束,您就可以解析表格的结构。此外,整个表都有整数内容。例如:

    # Using the requests HTTP client library
import requests
# Get data from HTTP request
data = requests.get("http://primes.utm.edu/lists/small/10000.txt").text
# Nested list comprehension: Split data into lines, consider from fourth line to second last, then split those lines into columns which will be evaluated as integers.
[[int(e) for e in l.strip().split()] for l in data.split('\n')[4:-2]]

瞧。

这是有效的,因为隐式 split 方法将在空格上进行拆分,例如制表符、空格组等。

关于python - 需要解析的异常表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36848910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com