python-plyfile icon indicating copy to clipboard operation
python-plyfile copied to clipboard

read error

Open rogerwarburton opened this issue 7 years ago • 5 comments

read gives error:

/usr/local/bin/python3.7 "/Users/roger/Documents/Projects/3D Faces/E4/test-ply-1.py" Traceback (most recent call last): File "/Users/roger/Documents/Projects/3D Faces/E4/test-ply-1.py", line 43, in rd = PlyData.read('Beethoven.ply' ) File "/usr/local/lib/python3.7/site-packages/plyfile.py", line 392, in read data = PlyData._parse_header(stream) File "/usr/local/lib/python3.7/site-packages/plyfile.py", line 373, in parse_header while parser.consume(stream.readline()): File "/usr/local/lib/python3.7/site-packages/plyfile.py", line 130, in consume getattr(self, 'parse' + keyword)(line[len(keyword)+1:]) File "/usr/local/lib/python3.7/site-packages/plyfile.py", line 138, in parse_ply self._error("unexpected characters after 'ply'") File "/usr/local/lib/python3.7/site-packages/plyfile.py", line 134, in _error raise PlyHeaderParseError(message, self.lines) plyfile.PlyHeaderParseError: line 1: unexpected characters after 'ply'

Configuration: OS X Mojave 10.14.1, Python 3.7, Pycharm Attached is the text file. Get same error if I use the test.txt file or test.ply Adding 'rb' gives: TypeError: read() takes 1 positional argument but 2 were given

Thanks Roger

rogerwarburton avatar Jan 12 '19 14:01 rogerwarburton

Is this the file: test.txt

For completeness, could you post a minimal test script to reproduce this error?

I realize I haven't tested this on Python 3.7, so I'm working on that. Unfortunately, I don't have OS X, but hopefully that isn't the issue.

dranjan avatar Jan 12 '19 20:01 dranjan

Yes, that is the test file. You just need two lines:

from plyfile import PlyData, PlyElement plydata = PlyData.read('test.ply')

rogerwarburton avatar Jan 12 '19 21:01 rogerwarburton

Okay, thanks. I was able to reproduce the error with the file you provided. The error appears to stem from the line ending convention ("\r", or Mac-style), which seems to trip up the readline method of the input file object in binary mode. I'll see if I can fix this, but meanwhile, as a workaround, if you can convert the line endings to Unix-style ("\n") or Windows-style ("\r\n"), plyfile should be able to read the file.

Here's a really quick-and-dirty Python script to normalize line endings. I think it should output Unix-style line endings on any platform.

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('input')
parser.add_argument('output')
args = parser.parse_args()

with open(args.input, 'r') as input_file:
    with open(args.output, 'wb') as output_file:
        for line in input_file:
            output_file.write(line.encode('utf8'))

This can only be done for ASCII-format PLY files, though. It will corrupt binary-format files.

dranjan avatar Jan 12 '19 23:01 dranjan

Thank you! Thank you! You always wonder if you are doing something stupid, so it is heartening to realize it is a real problem and, incidentally, that you may help someone else. Thanks for the prompt reply and the suggestion.

Regards Roger

Roger Warburton [email protected]

On Jan 12, 2019, at 6:47 PM, Darsh Ranjan [email protected] wrote:

Okay, thanks. I was able to reproduce the error with the file you provided. The error appears to stem from the line ending convention ("\r", or Mac-style), which seems to trip up the readline method of the input file object in binary mode. I'll see if I can fix this, but meanwhile, as a workaround, if you can convert the line endings to Unix-style ("\n") or Windows-style ("\r\n"), plyfile should be able to read the file.

Here's a really quick-and-dirty Python script to normalize line endings. I think it should output Unix-style line endings on any platform.

import argparse

parser = argparse.ArgumentParser() parser.add_argument('input') parser.add_argument('output') args = parser.parse_args()

with open(args.input, 'r') as input_file: with open(args.output, 'wb') as output_file: for line in input_file: output_file.write(line.encode('utf8')) This can only be done for ASCII-format PLY files, though. It will corrupt binary-format files.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/dranjan/python-plyfile/issues/21#issuecomment-453789917, or mute the thread https://github.com/notifications/unsubscribe-auth/ARQ5-GKhPE270ziu1FELuNvunaBhc9G6ks5vCnQTgaJpZM4Z8rNL.

rogerwarburton avatar Jan 12 '19 23:01 rogerwarburton

The hack works, thanks.

rogerwarburton avatar Jan 13 '19 22:01 rogerwarburton

Not that anyone was likely holding their breath for a proper fix for this issue...but it should be fixed on master now. It hasn't been packaged for PyPI yet, since I don't want to do too many releases too quickly. The release isn't far behind, though.

dranjan avatar Mar 22 '23 07:03 dranjan