admin管理员组

文章数量:1303670

Following situation: I want to load and read bufr files from a specific url without downloading it on my disk. That´s what i've done so far:

import requests
from io import BytesIO
import eccodes as ecc

my_url = ".bufr"

with requests.request(method='GET', url=my_url, headers={'cache-control': "no-cache"}) as response:
    if response.ok:

        with open(BytesIO(response.content), 'rb') as fp:  
            num_msgs = ecc.codes_count_in_file(fp)
            bid = ecc.codes_bufr_new_from_file(fp)
            print(num_msgs)
    else:
        print(f'{response.reason}')

This results into:

TypeError: expected str, bytes or os.PathLike object, not BytesIO

Has anyone an idea to change this code into a successful piece of text? :)

best regards.

Ich changed the code to:

import requests
from io import BytesIO
import eccodes as ecc

my_url = ".bufr"

with requests.request(method='GET', url=my_url, headers={'cache-control': "no-cache"}) as response:
    if response.ok:

        with BytesIO(response.content) as fp:  
            num_msgs = ecc.codes_count_in_file(fp)
            bid = ecc.codes_bufr_new_from_file(fp)
            print(num_msgs)
    else:

And it works. But it raises a neu error.

fileno Traceback (most recent call last): File "lib/python3.10/site-packages/gribapi/gribapi.py", line 435, in grib_count_in_file err = lib.grib_count_in_file(ffi.NULL, fileobj, num_p) io.UnsupportedOperation: fileno

本文标签: python 3xHow to open ioBytesIO() Object to read bufr files from urlStack Overflow