Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Allow different encodings for reading JCAMP files#101

Merged
jjhelmus merged 1 commit into
jjhelmus:masterfrom
andreasdoll:jcamp-encoding
Jul 22, 2019
Merged

Allow different encodings for reading JCAMP files#101
jjhelmus merged 1 commit into
jjhelmus:masterfrom
andreasdoll:jcamp-encoding

Conversation

@andreasdoll
Copy link
Copy Markdown
Contributor

Allow to provide a list of possible encodings for reading JCAMP files,
and use the first encoding which doesn't raise an UnicodeDecodeError.
Suppress such errors, except when all provided encodings fail.
Keep the current behaviour by choosing the default encoding if no
encoding is specified.

I'm not sure if you want to pull this, but for my purposes this is easier and more futureproof than converting the JCAMP files on operating system level.

Comment thread nmrglue/fileio/bruker.py Outdated
# JCAMP-DX functions

def read_jcamp(filename):
def read_jcamp(filename, encodings=[locale.getpreferredencoding()]):
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be preferable to use a single encoding here. If users want to try multiple encoding they can create a loop over read_jcamp with a check for a UnicodeDecodeError.

Comment thread nmrglue/fileio/bruker.py Outdated
warn("Unable to correctly parse line:" + line)
for N, encoding in enumerate(encodings):
try:
with open(filename, 'r', encoding = encoding) as f:
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Python 2 encoding is not an argument in the buildin open command. io.open does support this argument.

@jjhelmus
Copy link
Copy Markdown
Owner

@andreasdoll I made two suggestions on how I think this should be implemented. Let me know if you need any help with this.

Allow to specify an encoding for reading JCAMP files. Keep the current
behaviour by choosing the default encoding if no encoding is specified.
Use io.open() to read a JCAMP file to guarantee backwards compatibility
with Python 2.
@andreasdoll
Copy link
Copy Markdown
Contributor Author

Thanks @jjhelmus, I wasn't aware that encodings in open() is not backwards compatible. I've modified that commit according to your wishes and added the previously forgotten parameter docstring.

@jjhelmus
Copy link
Copy Markdown
Owner

LGTM, thanks @andreasdoll

@jjhelmus jjhelmus merged commit 51a655c into jjhelmus:master Jul 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants