简体   繁体   English

读取url内容,在python中进行操作

[英]Read url content and perform operation in python

I'm trying to get data from URL containing space-separated value line by line and perform some operations on each line.我正在尝试从 URL 逐行获取包含空格分隔值的数据,并在每一行上执行一些操作。

The URL contains: URL 包含:

586763334343 1523718907817 NullPointerException
586763434443 1523718907818 IllegalAgrumentsException
....

Here, 1st string is the request-id , 2nd string is a timestamp (in UTC) and the 3rd string is the Exception Name .在这里, 1st string is the request-id2nd string is a timestamp (in UTC)3rd string is the Exception Name

I'm trying to read each line from URL and convert the timestamp to date-time and append to a list such that my list looks something like this.我正在尝试读取 URL 中的每一行,并将时间戳转换为日期时间,将 append 转换为列表,这样我的列表看起来像这样。

[
 { second: 12, minute: 27, hour: 21, data: 'NullPointerException' },
 { second: 12, minute: 27, hour: 21, data: 'NullPointerException' }
]

How can I read the url and perform convert timestamp into date-time and append it to a list?如何读取 url 并将时间戳转换为日期时间并将 append 转换为列表?

I tried:我试过:

url = request.GET.get('url')
with closing(requests.get(url, stream=True)) as f:
    data = [list(map(int, line.split())) for line in f]
print(data)

I'm trying to read url in the following manner and I can't come up with solution to perform other operations.我正在尝试以下列方式读取 url,但我无法想出执行其他操作的解决方案。

Use,采用,

from datetime import datetime
import requests

url = "https://example/log1.txt"

resp = requests.get(url)
if resp.status_code == 200:
    data = []
    for line in resp.text.splitlines():
        split_ = line.split()
        # divide timestamp by 1000 to convert from milliseconds to seconds.
        date = datetime.utcfromtimestamp(int(split_[1]) / 1000)
        data.append({
            "second": date.second,
            "minute": date.minute,
            "hour": date.hour,
            "data": split_[-1],
        })

    print(data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM