Python Forum
Python 3.6.5 syntax error in IDLE
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 3.6.5 syntax error in IDLE
#1
Trying to use IDLE in Python 3.6.5 but receiving sytax error at the top line.

Python 3.6.5 (default, Jan 16 2019, 21:12:16) 
[GCC 7.3.0] on linux
Type "copyright", "credits" or "license()" for more information.
>>> import csv
>>> with open('/home/bob/Python_data_sets/names.csv', 'r') as csv_file:
	csv_reader = csv.reader(csv_file)
        for line in csv_reader:
                print(line)
Syntax error highlights the number 6 in the first line when I run "Check Module." Any thoughts?
Reply
#2
This question is better directed to the authors of python and idle at https://bugs.python.org/
Reply
#3
(May-08-2019, 08:42 PM)loren41 Wrote: Python 3.6.5 (default, Jan 16 2019, 21:12:16)
[GCC 7.3.0] on linux
Type "copyright", "credits" or "license()" for more information.
>>> import csv
>>> with open('/home/bob/Python_data_sets/names.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
for line in csv_reader:
print(line)

Is this the content of your .py file?
Or is this an interpretter session?
And if it's interpretter session then what "top line" do you actually mean? "import csv"?

To me it looks like you copied the contents of interpretter session into ".py" file and that is the reason why the error occurs.

The actual code (extracted from your post) that should be put into the ".py" file is:
import csv
with open('/home/bob/Python_data_sets/names.csv', 'r') as csv_file:
	csv_reader = csv.reader(csv_file)
    for line in csv_reader:
        print(line)
(didn't test it though)
Reply
#4
Tried a code example in IDLE first. Then tried a second example using Sublime Text. Same error, but more feedback.
Python 3.6.5 (default, Jan 16 2019, 21:12:16) 
[GCC 7.3.0] on linux
Type "copyright", "credits" or "license()" for more information.

>>> import csv  #import the csv module
#
>>> print(dir(csv)) # display the contents of the csv module
['Dialect', 'DictReader', 'DictWriter',
'Error', 'OrderedDict', 'QUOTE_ALL', 'QUOTE_MINIMAL', 'QUOTE_NONE',
'QUOTE_NONNUMERIC', 'Sniffer', 'StringIO', '_Dialect', '__all__',
'__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
'__name__', '__package__', '__spec__', '__version__', 'excel',
'excel_tab', 'field_size_limit', 'get_dialect', 'list_dialects', 're',
'reader', 'register_dialect', 'unix_dialect', 'unregister_dialect',
'writer']
#
>>> path = '/home/bob/Python_work_area/names.csv'
>>> file = open(path, newline = '')
#
# create a reader function to parse the csv data from the file
>>> reader = csv.reader(file)
#
# skip the header line and strip the data from the first data line
>>> header = next(reader)
>>> data = [row for row in reader] # read the remaining data
#
>>> print(header)
>>> print(data[0])
Error info appeared as follows:
File "/home/bob/Python_work_area/CSV_module.py", line 1
Python 3.6.5 (default, Jan 16 2019, 21:12:16)
^
SyntaxError: invalid syntax
[Finished in 0.0s with exit code 1]
[shell_cmd: python -u "/home/bob/Python_work_area/CSV_module.py"]
[dir: /home/bob/Python_work_area]
[path: /usr/bin:/usr/lib64/qt5/bin:/usr/share/kf5:/usr/libexec:/bin:/usr/bin:/usr/local/bin:/usr/games:/usr/lib64/qt5/bin:/usr/lib64/qt4/bin:/home/bob/bin:/sbin:/usr/sbin]

Have no idea what the problem is.
Reply
#5
Can you post the contents of the CSV_module.py file?
Reply
#6
Python 3.6.5 (default, Jan 16 2019, 21:12:16) 
[GCC 7.3.0] on linux
Type "copyright", "credits" or "license()" for more information.

>>> import csv  # import the csv module
#
>>> print(dir(csv)) # display the contents of the csv module
['Dialect', 'DictReader', 'DictWriter',
'Error', 'OrderedDict', 'QUOTE_ALL', 'QUOTE_MINIMAL', 'QUOTE_NONE',
'QUOTE_NONNUMERIC', 'Sniffer', 'StringIO', '_Dialect', '__all__',
'__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
'__name__', '__package__', '__spec__', '__version__', 'excel',
'excel_tab', 'field_size_limit', 'get_dialect', 'list_dialects', 're',
'reader', 'register_dialect', 'unix_dialect', 'unregister_dialect',
'writer']
#
>>> path = '/home/bob/Python_work_area/names.csv'
>>> file = open(path, newline = '')
#
# create a reader function to parse the csv data from the file
>>> reader = csv.reader(file)
#
# skip the header line and strip the data from the first data line
>>> header = next(reader)
>>> data = [row for row in reader] # read the remaining data
#
>>> print(header)
>>> print(data[0])
Reply
#7
You can not have text from IDLE/REPL which is interactive testing in a script.
In a script there is no >>> or start text of REPL Python 3.6.5 (default, Jan 16 2019, 21:12:16).

To show how it work as script,also a .py file.
# names.py
import csv
from pprint import pprint

pprint(dir(csv)) # display the contents of the csv module
['Dialect', 'DictReader', 'DictWriter',
'Error', 'OrderedDict', 'QUOTE_ALL', 'QUOTE_MINIMAL', 'QUOTE_NONE',
'QUOTE_NONNUMERIC', 'Sniffer', 'StringIO', '_Dialect', '__all__',
'__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
'__name__', '__package__', '__spec__', '__version__', 'excel',
'excel_tab', 'field_size_limit', 'get_dialect', 'list_dialects', 're',
'reader', 'register_dialect', 'unix_dialect', 'unregister_dialect',
'writer']

print('-'*20)
path = 'names.csv'
file = open(path, newline = '')
reader = csv.reader(file)
header = next(reader)
data = [row for row in reader]

print(header)
print(data[0])
Output:
E:\div_code\egg λ python names.py ['Dialect', 'DictReader', 'DictWriter', 'Error', 'OrderedDict', 'QUOTE_ALL', 'QUOTE_MINIMAL', 'QUOTE_NONE', 'QUOTE_NONNUMERIC', 'Sniffer', 'StringIO', '_Dialect', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__', 'excel', 'excel_tab', 'field_size_limit', 'get_dialect', 'list_dialects', 're', 'reader', 'register_dialect', 'unix_dialect', 'unregister_dialect', 'writer'] -------------------- ['first_name', 'last_name', 'email'] ['John', 'Doe', '[email protected]']
If i start REPL eg ptpython interact shell,and do first part it look this.
E:\div_code\egg
λ ptpython
>>> import csv

>>> dir(csv)[:10]
['Dialect', 'DictReader', 'DictWriter', 'Error', 'OrderedDict', 'QUOTE_ALL', 'QUOTE_MINIMAL', 'QUOTE_NONE', 'QUOTE_NONNUMERIC', 'Sniffer']
See now is >>> there,and command get executed bye Enter.
Reply
#8
Thank you Snippsat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why can I not see the code correctly in Python IDLE.? Trump 7 439 Mar-14-2024, 09:05 PM
Last Post: deanhystad
  Syntax error for "root = Tk()" dlwaddel 15 1,013 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 334 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,454 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,137 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  Launch Python IDLE Shell from terminal Pavel_47 5 1,143 Feb-17-2023, 02:53 PM
Last Post: Pavel_47
  syntax error question - string mgallotti 5 1,251 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,194 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 847 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python Idle won't start totalmachine 9 3,382 Oct-16-2022, 05:57 PM
Last Post: totalmachine

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020