1

I need to convert a WAV file to CSV file using a python script. I found on the internet a python package called wav2vec which converts the .wav file into a vector graphics file with a CSV formatter option.

When I load the wav2vec package into the terminal an then write directly the instructions I get the CSV file, but when I try to put it into a script and try to run it, I allways get a syntax error (invalid syntax)

Since a month ago I started programming with python, and when I saved "terminal instructions" or orders into a python script to later run it, I didn't have any problem. Is there any mistake that I'm missing?

First, I run my script with just the line "Import wav2vec"

Then, I go to the terminal and write:

wav2vec Maincra.wav --format CSV --width 1 --height 0 > Maincra.csv

Looking like this:

PS C:\Users\joker\OneDrive\Universidad\Proyecto Procesamiento de señales\Código> wav2vec Maincra.wav --format CSV --width 1 --height 0 > Maincra.csv

and it works! But when I try to put it all in the same script (in order to just press F5 and run everything), I get this error:

  File "c:\Users\joker\OneDrive\Universidad\Proyecto Procesamiento de señales\Código\Testwavtocsv.py", line 6
    wav2vec Maincra.wav --format CSV --width 1 --height 0 > Maincra.csv
                  ^
SyntaxError: invalid syntax

I don't know what to do to fix that, I have tried to write it in every possible way.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • Your first example is running a script from the Windows shell, which has absolutely nothing to do with Python. Try [spawning a subprocess](https://stackoverflow.com/questions/89228/calling-an-external-command-in-python). – Salem Oct 13 '19 at 16:35
  • can you share the full python script or the line where you invoke `wav2vec` – rok Oct 13 '19 at 16:35
  • You can execute simple "terminal instructions" using [`os.system()`](https://docs.python.org/3/library/os.html#os.system). If you want to redirect the output, you'll need to use the `subprocess` module. – martineau Oct 13 '19 at 16:40
  • You're mixing up PowerShell and Python. Can you give us the link where you found wav2vec package? – ba_ul Oct 13 '19 at 16:44
  • There it is the link where I found wav2vec package: https://pypi.org/project/wav2vec/ – Miguel Villalobos Oct 13 '19 at 18:09
  • Thank you so much martineau, with the os.system() I was able to automate the process I've described – Miguel Villalobos Oct 13 '19 at 18:19
  • @martineau: Please don’t recommend `system`—it’s the wrong choice for every application except interactive things like `!` in `less`. – Davis Herring Oct 13 '19 at 20:25
  • @DavisHerring: There's nothing wrong with using it if it's all you need. – martineau Oct 13 '19 at 21:20
  • Miguel: Since there's a pypi python package to interface with wav2vec, you don't need to use `os.system()` (or the `subprocess` module) to run the command-line ("terminal") version. Instead you can do what you need to completely within your python script. See the paragraph that starts with "You can also \`import wav2vec\` in order to convert wave files to the supported output formats in your own Python scripts" in the wav2vec [**Project description**](https://pypi.org/project/wav2vec/). Note however that the indentation is incorrect in the sample Python code following it. – martineau Oct 13 '19 at 21:32
  • @martineau: It ignores `SIGINT`, forces you to run the shell (conflating failure to run the command and the command failing and adding complexity and security concerns), and encodes the exit status in an unfamiliar fashion that’s all too easy to ignore entirely. What’s not to like? – Davis Herring Oct 13 '19 at 22:40

0 Answers0