在Python中打开一个wave文件:未知格式:49.出了什

浏览次数:89 时间:2020-07-18

  I try to open a wave file with the wave module, but I keep getting the same error whatever I try.

  The line with the error is the following:

  wav=wave.open(f)

  This is the error message:

  Traceback (most recent call last):

  File "annotate.py", line 47, in

  play(file)

  File "annotate.py", line 33, in play

  wav=wave.open(f)

  File "C:\Program Files (x86)\Python\lib\wave.py", line 498, in open

  return Wave_read(f)

  File "C:\Program Files (x86)\Python\lib\wave.py", line 163, in __init__

  self.initfp(f)

  File "C:\Program Files (x86)\Python\lib\wave.py", line 143, in initfp

  self._read_fmt_chunk(chunk)

  File "C:\Program Files (x86)\Python\lib\wave.py", line 269, in _read_fmt_chunk

  raise Error('unknown format: %r' % (wFormatTag,))

  wave.Error: unknown format: 49

  String f is a path to a .WAV file and it works when played in any of my media players.

  I have of course imported the wave module.

  I tried f both as a relative and an absolute path.

  I tried replacing "WAV" by "wav".

  What is the error caused by?

  Python's wave module works with a specific type of WAV: PCM (WAVE_FORMAT_PCM: 0x0001).

  In your case, you're using a WAV of type WAVE_FORMAT_GSM610 [0x0031=hex(49)].

  You can use a program like Audacity or some lib for converting codecs to change the type of the WAV file.

  You can see a list of WAV types here:

  https://www.videolan.org/developers/vlc/doc/doxygen/html/vlc__codecs_8h.html

  Python's wave module source code:

  https://github.com/python/cpython/blob/master/Lib/wave.py

  I'm working with DecimalFormat, I want to be able to read and write decimals with as much precision as given (I'm converting to BigDecimal).

  Essentially, I want a DecimalFormat which enforces the following pattern "\d+(\.\d+)?" i.e. "at least one digit then, optionally, a decimal separator followed by at least one digit".

  I'm struggling to be able to implement this using DecimalFormat, I've tried several patterns but they seem to enforced fixed number of digits.

  I'm open to alternative ways of achieving this too.

  Edit:

  For a little more background, I'm parsing user-supplied data in which decimals could be formatted in any way, and possibly not in the locale format. I'm hoping to let them supply a decimal format string which I can use the parse the data.

  Since you noted in a comment that you need Locale support:

  Locale locale=//get this from somewhere else

  DecimalFormat df=new DecimalFormat();

  df.setDecimalFormatSymbols(new DecimalFormatSymbols(locale));