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
\nand I got it working. By working...I did not receive a syntax error. That is all I checked as far as functionality.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 necessarypython3 -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 charactercsvfile.seek(0).