从urlu库中删除某些行

2024-04-20 06:38:49 发布

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

import csv
import urllib.request
from pylab import *

eventurl = "http://data.hisparc.nl/show/source/eventtime/501/2017/1/1/"
data = urllib.request.urlopen(eventurl)
print(data.read())

当我运行代码并打印eventurl中的数据时,终端显示如下:

b"# HiSPARC eventtime histogram source\n#\n# Station: 501\n# Data from 2017-1-1\n#\n# HiSPARC data is licensed under Creative Commons Attribution-ShareAlike 4.0.\n#\n#\n# Please note: the 'bin' is the left bin edge. The width of the bin is 1\n# hour.  So bin 0 means between 0:00 and 1:00. Value means the number of\n# events which were measured during 1 hour.\n#\n# This data contains the following columns:\n#\n# bin:   time [hour of day]\n# value: number of events [counts]\n#\n#\n# bin\tvalue\n0\t2265\n1\t2354\n2\t2302\n3\t2353\n4\t2369\n5\t2378\n6\t2280\n7\t2411\n8\t2340\n9\t2431\n10\t2353\n11\t2394\n12\t2412\n13\t2470\n14\t2404\n15\t2540\n16\t2492\n17\t2390\n18\t2454\n19\t2404\n20\t2451\n21\t2467\n22\t2471\n23\t2371\n\n"

我们想要的,只是两行的箱子和值。有没有一个代码可以帮助我们摆脱其余的,并使它易于使用的直方图的数字?你知道吗


Tags: ofthe代码fromimportsourcedatabin
2条回答
data = data.read().decode()
values = data.split("# bin  value")

result = [d.split("\t")[1] for d in values[1].strip().split("\n")]
print(result)

输出:

['2265', '2354', '2302', '2353', ...., '2471', '2371']

使用Python regexre模块

import csv
import urllib.request
import re
l = []
eventurl = "http://data.hisparc.nl/show/source/eventtime/501/2017/1/1/"
data = urllib.request.urlopen(eventurl)
for line in data.readlines():
    line = str(line,'utf-8').strip()
    if re.search(r'\d+\t\d+', line):
        l.append(line.split()[1])

print (l)

输出:

['2265', '2354', '2302', '2353', '2369', '2378', '2280', '2411', '2340', '2431', '2353', '2394', '2412', '2470', '2404', '2540', '2492', '2390', '2454', '2404', '2451', '2467', '2471', '2371']

相关问题 更多 >