473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

invalid syntax printing string from file

running 2.7.5
on Mac
from the command prompt --
>>> Print "foo"
foo

works fine.

from script file
--
#! /usr/bin/env python
#encoding:latin-1
print “foo”
--

get following error:
dans-air:~ dan$ python ./Documents/Python/Samples/foo.py
File "./Documents/Python/Samples/foo.py", line 3
print ?foo?
^
SyntaxError: invalid syntax


since this is 2.x Print "foo" should work but tried Print ("foo") get same error on quote mark.


file is saved as latin-1 encoding added encoding lines #.

can print integer from this file, print 6 results in 6 when file runs.


thanks,
Banging my head against the wall
Nov 9 '14 #1
4 2189
dwblas
626 Expert 512MB
Python only recognizes single or double quote marks, ord 34 or 39, for strings. Note the difference in the quote marks between
>>> P(p)rint "foo"
and
print “foo”
Expand|Select|Wrap|Line Numbers
  1. """ #! /usr/bin/env python
  2.  #encoding:latin-1
  3. """
  4. s='“foo”'
  5. print ord(s[0])
  6. print ord(s[-1])
  7. print ord('"')
  8. print ord("'") 
Nov 9 '14 #2
used both single quote and double quote (not both at same time) both result in same error:

case 1:
#! /usr/bin/env python
#encoding:latin-1
print ‘foo’

dans-air:~ dan$ python ./Documents/Python/Samples/foo.py
File "./Documents/Python/Samples/foo.py", line 3
print ?foo?
^
SyntaxError: invalid syntax


case 2:
#! /usr/bin/env python
#encoding:latin-1
print “foo”

dans-air:~ dan$ python ./Documents/Python/Samples/foo.py
File "./Documents/Python/Samples/foo.py", line 3
print ?foo?
^
SyntaxError: invalid syntax

the quote kicks out the ? syntax error. as if the character is not recognized. I am encoding so not sure what is missing.

thanks
Nov 10 '14 #3
bvdet
2,851 Expert Mod 2GB
See if this makes any difference:

# coding=latin-1

OR

# -*- coding: latin-1 -*-
Nov 10 '14 #4
bcdet,
tried both still kicks out same error:

File "./Documents/Python/Samples/foo.py", line 3
print ?foo?
^
SyntaxError: invalid syntax


not recognizing the "

thanks
Nov 11 '14 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: DIBS | last post by:
I'm new to Python and I don't understand what I'm doing wrong. I'm running windows xp. In the command line window, I type: Python Sudoku.py and I get the response" SyntaxError: invalid...
2
by: py | last post by:
Hi, I am running python 2.4.2 on win xp pro. I have the WMI module from Tim Golden (http://tgolden.sc.sabren.com/python/wmi.html). I have some code which does this... MyScript.py...
0
by: cesare | last post by:
hi, i'm very new to python and am experiencing the following problem: an identical program that runs on certain computers crashes on other machines due to Carriage Returns (CR; 0x0a) that now...
5
by: Ross | last post by:
I have a quick question regarding printing string constants. When you printf() a string constant, how are the characters encoded that get sent to the standard output? Is it in the execution...
10
by: ronrsr | last post by:
no matter where I place this imported file,the statement after it in the main program gets a syntax error, regardless of the syntax. I think I may have changed something in this file, but I'm...
10
by: jonathanemil | last post by:
Hello, I am a 1st semester Computer Science student in a Python class. Our current assignment calls for us to read a list from a file, create a 2-dimensional list from the file, and check to see...
7
by: alf | last post by:
Hi, I wonder why it is an invalid syntax: File "<stdin>", line 1 if 1: if 1: if 1: print 1 or
6
by: Nathan Pinno | last post by:
Why does my compiler say invalid syntax and then highlight the quotation marks in the following code: # This program is to find primes. primes = import math import gmpy while 1: run =...
1
by: Jake Colborn | last post by:
Hey, I just picked up python again after not coding with it for many years. But I wanted to throw some simple scripts together to see if I remember all the functionality so far. Now I've got this...
1
by: Karen Rosen | last post by:
This is the first time I am attempting this - someone was kind enough to help me with my previous error and told me to add print statements before the error which I did the error is now invalid...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.