Hello to everyone!
Yes, Im a newbie and I have a problem that is kind of a pain.
I receive an error "Invalid Syntax", when trying to run this.
The file mesaure.csv has been created and it is locate on the correct folder with the .py file. Whe I try tho run it I get the invalid syntax error on the last "row"
Im using Python 3.1.2 and IDLE or Eclipse.
Anyone can give a hand on this and tell me what it is that Im doing wrong??

Thanks

#!usr/bin/python
#Filename:


import csv
reader = csv.reader(open("mesaure.csv", "rb"))
for row in reader:
print row

Dani AI

Generated

The SyntaxError on print row is because you are running Python 3.x, where print is a function, not a statement. Use print(row) and, when reading CSV in Python 3, open the file in text mode with newline='' so the csv module can handle newlines correctly.

Example (Python 3.x):

import csv

with open('mesaure.csv', 'r', newline='') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)

This newline='' requirement is documented in the Python 3 csv module docs. Docs: csv reader/writer usage. Also see that print is explicitly a function in Python 3. PEP 3105: print as a function.

Why it works in your terminal but not in IDLE/Eclipse: they are likely using different interpreters. On many Ubuntu setups from that era, python runs Python 2.x while python3 runs Python 3.x, and editors can be configured to either. Check both environments:

  • In terminal: run python --version and python3 --version.
  • In your editor/IDE, confirm the configured interpreter matches what you expect (e.g., PyDev Interpreter settings).

Historically, the python command could map to Python 2 or 3 depending on the distro, which explains this mismatch. PEP 394: the "python" command on Unix.

Practical tips:

  • If launching from the shell, add a shebang like #!/usr/bin/env python3 at the top of your script.
  • Path issues can differ between terminal and IDEs; either run the script from the folder containing mesaure.csv or build the path relative to the script file.

Recommended Answers

All 7 Replies

Dont forget to select your code and press (CODE) next time.

And do you have a sample file, becasue this normally works.

import csv

for row in csv.reader(open('mesaure.csv')):
    print row

This should work, right?? ... I have tried a few things but still not working.
sorry about the code thing .. now I did :)

import csv
reader = csv.reader(open("mesaure.csv", "rb"))
for row in reader:
    print row

After running the file on a Terminal I get the results I want but is no working when running the same file, , on the editor. I have change the name from 'mesaure' to measured. This is what I get from the terminal:

carlos@ubuntu:~$ python

Why can't I run this same file that I run from the terminal on my editors??
That is the question.

Would it maybe easier to use my code snippet for text file based database (actualy card file would be more proper term)

Try it with the complete path name.

reader = csv.reader(open("/path/to/mesaure.csv", "rb"))

After running the file on a Terminal I get the results I want but is no working when running the same file, , on the editor.

This is too vague to comment on. What does "not working" mean?

Hello,
Anyone to help on this error?

File "./pyes2csv.py", line 54

        with open('esdata.csv', 'w', newline='') as csvfile:   
           ^
           SyntaxError: invalid syntax

My line of code:

    with open('esdata.csv', 'w', newline='') as csvfile:   
          filewriter = csv.writer( csvfile, delimiter='\t',  
                        quotechar='|', quoting=csv.QUOTE_MINIMAL)
        filewriter.writerow(["@timestamp", "session_event", "to_addr", "mno", "from_addr"])    

Thanks

you may be using an old version of python without a with statement. Run python --version to check.

Also don't revive old forum threads. Start your own thread instead if you have questions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.