-5

I am getting an Indentation error,while executing below code.

import pyodbc
import shutil
import pandas as pd
import numpy as np

def My_function():
data = pd.read_excel(r'my excel path')

    dataincsv = data.to_csv(r'export into my csv path',sep=r'|')

    cnxn = pyodbc.connect('''connection string''')
    stmt1 =  """Select column 1 from mytable"""
        try:
            Out_service = pd.read_sql(stmt1,cnxn)
        except:
            print("File format might be wrong,check the error")
        else:
            print(Out_service)
            exit()

Getting below error when i run the code

line 14 try: ^ IndentationError: unexpected indent

khelwood
  • 55,782
  • 14
  • 81
  • 108
Rahul
  • 467
  • 1
  • 8
  • 24
  • 1
    Why is your `try`-`except`-`else` block indented? It should not be. – Ziyad Edher Jun 26 '18 at 19:02
  • Hi Ziyad,Thanks for quick rply I removed the block indentation,this time i am getting unable to impaort 'pandas' import pandas as pd import pyodbc import shutil import numpy as np def My_ListSource(): dataincsv = data.to_csv(r'export into my csv path',sep=r'|') cnxn = pyodbc.connect('''connection string''') stmt1 = """Select column 1 from mytable""" try: Out_service = pd.read_sql(stmt1,cnxn) except: print("File format might be wrong,check the error") else: print(Out_service) exit() – Rahul Jun 26 '18 at 19:09
  • And are you still getting the same error? – Ziyad Edher Jun 26 '18 at 19:10
  • 3
    Possible duplicate of [What to do with "Unexpected indent" in python?](https://stackoverflow.com/questions/1016814/what-to-do-with-unexpected-indent-in-python) – khelwood Jun 26 '18 at 19:13
  • your `data = pd.read_excel(r'my excel path')` needs indent. see my answer below. And please do read above link posted by @khelwood – pyeR_biz Jun 26 '18 at 19:16

3 Answers3

2

If the code you've pasted here is the exactly one you're using, there's no reason to indent the try.

It should be like:

dataincsv = data.to_csv(r'export into my csv path',sep=r'|')

cnxn = pyodbc.connect('''connection string''')
stmt1 =  """Select column 1 from mytable"""
try:
    Out_service = pd.read_sql(stmt1,cnxn)
except:
    print("File format might be wrong,check the error")
else:
    print(Out_service)
    exit()

The try block should at the same level as the previous line.

EDIT: I see that you updated your code, so my answer is a bit incomplete, but you still have problems in your indentation.

Jonatas CD
  • 878
  • 2
  • 10
  • 19
  • I didn't understand why my answer lost the status of 'Accepted answer'. It reflects the state of the question by the time I answered it. I've even added an 'Edit' update to make sure why my solution wasn't updated to the latest state of the question content. Anyway... let's move on. – Jonatas CD Jul 02 '18 at 16:28
0
def My_function():
    data = pd.read_excel(r'my excel path')
    dataincsv = data.to_csv(r'export into my csv path',sep=r'|')

    cnxn = pyodbc.connect('''connection string''')
    stmt1 =  """Select column 1 from mytable"""
    try:
        Out_service = pd.read_sql(stmt1,cnxn)
    except:
        print("File format might be wrong,check the error")
    else:
        print(Out_service)
        exit()
pyeR_biz
  • 986
  • 12
  • 36
0

Here is how the function should look as far as indentation, also in some text editors if you indent with Tab and spaces it can cause an issue. If the issue still persists it's most likely due to that. Hope this helps, happy coding!

def My_function():
    data = pd.read_excel(r'my excel path')
    dataincsv = data.to_csv(r'export into my csv path',sep=r'|')

    cnxn = pyodbc.connect('''connection string''')
    stmt1 =  """Select column 1 from mytable"""
    try:
        Out_service = pd.read_sql(stmt1,cnxn)
    except:
        print("File format might be wrong,check the error")
    else:
        print(Out_service)
        exit()
wowwee
  • 176
  • 1
  • 2
  • 12