Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error if no charset encoding in response headers #259

Closed
ae3e opened this issue Oct 16, 2018 · 5 comments
Closed

Error if no charset encoding in response headers #259

ae3e opened this issue Oct 16, 2018 · 5 comments
Labels

Comments

@ae3e
Copy link
Contributor

ae3e commented Oct 16, 2018

hi,

I tried to read a las file from a url in Jupyter.
In the response headers, the content-type is only text/plain. There's no charset.

Then I got the following error :

/opt/conda/lib/python3.6/site-packages/lasio/reader.py in open_file(file_ref, **encoding_kwargs)
     79                 response = urllib.request.urlopen(file_ref)
     80                 encoding = response.headers.get_content_charset()
---> 81                 file_ref = StringIO(response.read().decode(encoding))
     82                 logger.debug('Retrieved data decoded via {}'.format(encoding))
     83         elif len(lines) > 1: # it's LAS data as a string.

TypeError: decode() argument 1 must be str, not None

Could it be possible to use the define enconding like this :

lasio.read('http://myserver/myfile.las',encoding='windows-1252')
@kinverarity1
Copy link
Owner

Could you post a code sample with a URL that is producing the error for you? I am not getting any errors using Python 3.6:

Python 3.6.3 |Anaconda, Inc.| (default, Nov  8 2017, 15:12:41) [MSC v.1900 32 bit (Intel)]
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import lasio

In [2]: lasio.__version__
Out[2]: '0.22'

In [3]: !git log --oneline -n 3
2911da1 (HEAD, tag: v0.22) v0.22
1d36ae3 Merge pull request #249 from krzysztof-slowinski/master
fdaccb5 Merge pull request #252 from euclidity/master

In [4]: l = lasio.read('https://raw.githubusercontent.com/kinverarity1/lasio/master/tests/examples/2.0/sample_2.0.las')

Thanks for the report!

@ae3e
Copy link
Contributor Author

ae3e commented Oct 17, 2018

Unfortunatly, it is a URL on my company's network so I can't give you a code sample with a URL. But, when I request both URLs with Postman, I can see a difference in the response headers which could explain the error :

Your file :
image

My file :
image

@kinverarity1
Copy link
Owner

kinverarity1 commented Oct 17, 2018 via email

@ae3e
Copy link
Contributor Author

ae3e commented Oct 18, 2018

To solve my problem, I replaced the line

https://github.com/kinverarity1/lasio/blob/master/lasio/reader.py#L80

with the following code :

if response.headers.get_content_charset() is None:
  if 'encoding' in encoding_kwargs:
    encoding=encoding_kwargs['encoding']
  else :
    encoding='utf-8'
else:
  encoding = response.headers.get_content_charset()

Tell me if you want a PR

@kinverarity1
Copy link
Owner

kinverarity1 commented Oct 18, 2018 via email

ae3e added a commit to ae3e/lasio that referenced this issue Oct 18, 2018
kinverarity1 added a commit that referenced this issue Oct 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants