2

I know it would be easier if I put a long python script within a file, but I am trying to resolve why I am having a syntax error when within a python3 -c cmd:

FYI: If it matters, I am performing this on OS X.

I read the following, but it did not resolve my problem: python multiline command running from bash

This is the working python3 script if executed from within a file:

import csv

with open('tmp/google_adwords_report.csv', 'r') as csvfile:
    csvfile.seek(0)
    next(csvfile)
    readCSV = csv.DictReader(csvfile)
    for row in readCSV:
        print(row)

And this is the same script if performed within command line python3 -c cmd:

$ python3 -c "import csv;  with open('tmp/google_adwords_report.csv', 'r') as csvfile: csvfile.seek(0); next(csvfile);  readCSV = csv.DictReader(csvfile); for row in readCSV: print(row);"

  File "<string>", line 1
    import csv;  with open('tmp/google_adwords_report.csv', 'r') as csvfile: csvfile.seek(0); next(csvfile);  readCSV = csv.DictReader(csvfile); for row in readCSV: print(row);
                    ^
SyntaxError: invalid syntax

Thanks

7
  • 1
    Based on the link you provided, you aren't exactly following the instructions in the working solution. I followed them and the only issue I had before getting it to work were indentation issues, which wasn't surprising. I tried out spacing accordingly after each\n and I got it working. By working...I did not receive a syntax error. That is all I checked as far as functionality. Commented Mar 23, 2016 at 22:09
  • 2
    Unrelated but csvfile.seek(0) is a noop, the pointer is already at the start of the file, also if you exec the line in an ide you will get the same error. I got it working correctly by adding tabs and newlines where necessary Commented Mar 23, 2016 at 22:17
  • @idjaw I tried: python3 -c "import csv\n\nwith open('tmp/google_adwords_report.csv', 'r') as csvfile:\n\tnext(csvfile)\n\treadCSV = csv.DictReader(csvfile)\n\tfor row in readCSV:\n\t\tprint(row)", and I got SyntaxError: unexpected character after line continuation character Commented Mar 23, 2016 at 22:30
  • 1
    @JeffTanner That means your spacing after the new lines is still off. This did not give me a syntax error: pastebin.com/WPPaFKEx Commented Mar 23, 2016 at 22:32
  • @PadraicCunningham Good point about the csvfile.seek(0). Commented Mar 23, 2016 at 22:34

2 Answers 2

2

You are limited by what is allowed in Python on a single line. See the Compound Statements section of the Python Language Ref for more info.

Basically, some things are not allowed. For example:

python -c "if (1): print(1)"

is okay. But:

python -c "if (1): if (1): print(1)"

is not allowed.

Another thing, you can't combine a statement with a indented clause. So, this is also not okay:

python -c "print(1); if (1): print(1)"

And that's the issue you are running into.

If you're in a shell that supports continuation of strings, you could do:

# python3 -c "if (1):
>   if (1):
>     print(1)
> "
1

Another thing that may work for you is inline CRs (via ^V^M). For example:

# python3 -c "if (1):^M  if (1):^M    print(1)"
1

That is, I actually typed in CTRL-V CTRL-M to create the ^M.

And, I am on OS X as well (Mavericks), running from bash.

Sign up to request clarification or add additional context in comments.

Comments

1

Tyring to do this in a single line is wasting your time, and will cause you grief when you need to make a change to your code. This would be true in any language, but is especially so due to Python's whitespace requirements.

Just use a heredoc:

python3 << EOF
import csv

with open('tmp/google_adwords_report.csv', 'r') as csvfile:
    csvfile.seek(0)
    next(csvfile)
    readCSV = csv.DictReader(csvfile)
    for row in readCSV:
        print(row)
EOF

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.