0

I keep getting random indentationError using OS X and Pycharm (Tried switching tabs and spaces in pycharm settings no luck). If I run the project under linux it works just fine. The below code attempts to upload the users from a csv, I've attempted to comment and different fields that dont exist in this group of users.

import csv

from django.contrib.auth import get_user_model

User = get_user_model()

members = open('Volunteers.csv', "rU")
data = csv.DictReader(members)

default_password = User.objects.make_random_password()


def generate_username(first_name, last_name):
    val = "{0}{1}".format(first_name[0], last_name[0]).lower()
    x = 0
    while True:
        if x == 0 and User.objects.filter(username=val).count() == 0:
            return val
        else:
            new_val = "{0}{1}".format(val, x)
            if User.objects.filter(username=new_val).count() == 0:
                return new_val
        x += 1
        if x > 1000000:
            raise Exception("Name is super popular!")


for row in data:
    email = row['Email']
    first_name = row['First Name'],
    last_name = row['Last Name'],
    username = generate_username(first_name, last_name)
    user = User.objects.create_user(username, email, default_password)
    user.is_staff = False
    user.volunteer = True
    user.active = row['Active']
    user.first_name = row['First Name']
    user.last_name = row['Last Name']
    user.organization = row['Organization']
    user.interview = row['Interview']
    user.house_number_street_name = row['Address']
    user.state = row['State']
    user.city = row['City']
    user.zip_code = row['Zip Code']
    user.daytime_phone = row['Daytime Phone']
    user.home_phone = row['Home Phone']
    user.cell_phone = row['Cell Phone']
    user.organization = row['Organization']
    user.emergency_contact = row['Emergency Contact']
    user.days_available = row['Days Available']
    user.food_pantry = row['Food Pantry']
    user.interview = row['Interview']
    user.bi_lingual = bool(row['Bilingual'])
    user.fund_raising = row['Fund Raising']
    user.board_member = row['Board Member']
    user.sunshine_committee = row['Sunshine Committe']
    user.solicit_donations = row['Solicit Donations']
    user.record_keeping = row['Record Keeping']
    user.truck_or_van = row['Truck or Van']
    user.pick_up_food = row['Pick up Food']
    user.deliver_food = row['Deliver Food']
    user.save()
    # user.comments = row['Comments']
    # user.drivers_licence = row['Drivers Licence Number']
    # user.unemployment = row['Unemployment']
    # user.food_stamps = row['Food Stamps']
    # user.disability = row['Disability']
    # user.salary = row['Salary']
    # user.pension = row['Pension']
    # user.ss_ssi = row['Social and Supplemental Income']

The logs

IndentationError: unexpected indent
>>>     user.record_keeping = row['Record Keeping']
  File "<console>", line 1
    user.record_keeping = row['Record Keeping']
    ^
IndentationError: unexpected indent
>>>     user.truck_or_v    user.truck_or_v    ]
  File "<console>", line 1
    user.truck_or_v    user.truck_or_v    ]
    ^
IndentationError: unexpected indent
>>>     user.pick_up_food = row['Pick up Food']
  File "<console>", line 1
    user.pick_up_food = row['Pick up Food']
    ^
IndentationError: unexpected indent
>>>     user.deliver_food = row['Deliver Food']
  File "<console>", line 1
    user.deliver_food = row['Deliver Food']
    ^
IndentationError: unexpected indent
>>>     user.save()
  File "<console>", line 1
    user.save()
    ^

Updated code that will run in reference to comment below asking to comment out lines with errors.

import csv

from django.contrib.auth import get_user_model

User = get_user_model()

members = open('Volunteers.csv', "rU")
data = csv.DictReader(members)

default_password = User.objects.make_random_password()


def generate_username(first_name, last_name):
    val = "{0}{1}".format(first_name[0], last_name[0]).lower()
    x = 0
    while True:
        if x == 0 and User.objects.filter(username=val).count() == 0:
            return val
        else:
            new_val = "{0}{1}".format(val, x)
            if User.objects.filter(username=new_val).count() == 0:
                return new_val
        x += 1
        if x > 1000000:
            raise Exception("Name is super popular!")


for row in data:
    email = row['Email']
    first_name = row['First Name'],
    last_name = row['Last Name'],
    username = generate_username(first_name, last_name)
    user = User.objects.create_user(username, email, default_password)
    user.is_staff = False
    user.volunteer = True
    user.active = row['Active']
    user.first_name = row['First Name']
    user.last_name = row['Last Name']
    user.organization = row['Organization']
    user.interview = row['Interview']
    user.house_number_street_name = row['Address']
    user.state = row['State']
    user.city = row['City']
    user.zip_code = row['Zip Code']
    user.daytime_phone = row['Daytime Phone']
    user.save()

    # user.home_phone = row['Home Phone']
    # user.cell_phone = row['Cell Phone']
    # user.organization = row['Organization']
    # user.emergency_contact = row['Emergency Contact']
    # user.days_available = row['Days Available']
    # user.food_pantry = row['Food Pantry']
    # user.interview = row['Interview']
    # user.bi_lingual = bool(row['Bilingual'])
    # user.fund_raising = row['Fund Raising']
    # user.board_member = row['Board Member']
    # user.sunshine_committee = row['Sunshine Committe']
    # user.solicit_donations = row['Solicit Donations']
    # user.record_keeping = row['Record Keeping']
    # user.truck_or_van = row['Truck or Van']
    # user.pick_up_food = row['Pick up Food']
    # user.deliver_food = row['Deliver Food']
    # user.save()
    # user.comments = row['Comments']
    # user.drivers_licence = row['Drivers Licence Number']
    # user.unemployment = row['Unemployment']
    # user.food_stamps = row['Food Stamps']
    # user.disability = row['Disability']
    # user.salary = row['Salary']
    # user.pension = row['Pension']
    # user.ss_ssi = row['Social and Supplemental Income']
ncrmro
  • 177
  • 2
  • 12
  • 1
    If you comment out all of those lines throwing the error, does it complain about anything else? – arewm Jun 08 '16 at 16:07
  • @arewm yes I've edited the with the code that will run with the rest of the fields commented out. – ncrmro Jun 08 '16 at 16:20
  • Possible duplicate of [What to do with "Unexpected indent" in python?](http://stackoverflow.com/questions/1016814/what-to-do-with-unexpected-indent-in-python) – rnevius Jun 08 '16 at 16:28
  • I know what this might be, one text editor may read everything as spaces or tabs and another might be reading it as a mix, so then the indent is incorrect. – jgr208 Jun 08 '16 at 16:28
  • I've opened it in sublime, xcode, textwrangler in addition to the pycharm text editor. Then instead of using the built in terminal I tried with os x's terminal and I can copy and paste in no problem. Sometimes the code will paste fine into pycharm terminal sometimes not. – ncrmro Jun 08 '16 at 16:40
  • 1
    Try running the original code with the `-tt` option: `python3 -tt test.py` to see if Python is confused between spaces/tabs. If each line in `for row in data:` is using spaces and then the ones you have commented out are using tabs, you can get the unexpected indent error. – arewm Jun 08 '16 at 17:20
  • @arewm doesn't appear to return anything. – ncrmro Jun 08 '16 at 19:19
  • 1
    @ncrmro It doesn't return anything when running it in linux or OSX/Pycharm? That was my last suggestion. Are you using 4 spaces for each intentation? Just for the fun of it, comment out all lines in hte `for row in data:` block except for one that always works and one that you have to comment out to run in OSX/Pycharm. Add 4 more spaces to the indentation for the line that always works while leaving the sometimes-working line the same. – arewm Jun 08 '16 at 19:28

0 Answers0