0

I am trying to build up a wine recommendation system. I was trying import data from CSV using pandas and I get this error:

C:\Users\Adhista Chapagain\Desktop\winerama> python load_users.py data/users.csv   File "load_users.py", line 22
    print "Reading from file " + str(sys.argv[1])
                             ^ SyntaxError: invalid syntax

Here is the code for load_users.py:

import sys, os 
import pandas as pd

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "winerama.settings")

import django
django.setup()

from django.contrib.auth.models import User


def save_user_from_row(user_row):
    user = User()
    user.id = user_row[0]
    user.username = user_row[1]
    user.save()


if __name__ == "__main__":

    if len(sys.argv) == 2:
        print "Reading from file " + str(sys.argv[1])
        users_df = pd.read_csv(sys.argv[1])
        print users_df

        users_df.apply(
            save_user_from_row,
            axis=1
        )

        print "There are {} users".format(User.objects.count())

    else:
        print "Please, provide User file path"
user812786
  • 4,302
  • 5
  • 38
  • 50
adhistac
  • 49
  • 6
  • 1
    What is your python version? – kuro Jun 12 '17 at 16:30
  • Your SyntaxError is coming from the executing this in Python3, where `print` requires parenthesis `print( ... )`. Either run this as Python2, or wrap your print in parenthesis – Wondercricket Jun 12 '17 at 16:39

2 Answers2

0

It might be that you're running python3, for which print is a function and must be surrounded by brackets:

        print("Reading from file", str(sys.argv[1]))

You can get sure of which version you have by running:

python --version
Javier
  • 161
  • 8
-1

SyntaxError in Python always caused by the mixed use of blank and Tab, make sure you use only one of them. Is there is space before "import sys, os" in your code ? If so, delete the space.

chenxingwei
  • 251
  • 1
  • 9
  • There was space and i deleted it but still i am getting this error....#Chenxingwei – adhistac Jun 12 '17 at 16:27
  • The space/tabs are not the problem. If they truly were, then the OP would be getting an [IndentionError](https://docs.python.org/2/library/exceptions.html#exceptions.IndentationError) or [TabError](https://docs.python.org/2/library/exceptions.html#exceptions.TabError) – Wondercricket Jun 12 '17 at 16:28
  • There is error in the syntax but I cannot figure it out..:( – adhistac Jun 12 '17 at 16:30