Question

I've got one more novice question:

I've got numerous links to external files and some of the directorate names are quite long (due to original folder structure). I've tried numerous methods for breaking a line, but most of them fails while using it with conjunction with pyodbc module.

So far I've got:

SIMD = xlrd.open_workbook(r'P:\Costing and Income\Projects & Planning\HRG, '\
    'IRF, Programme Budgeting\__2008-11\Developments\SIMD\PI_upload (08.05.2012).xls')

Which works OK for xlrd module

Tried some simple stuff directly in the IDLE:

>>> a = 'some text'\
    'more stuff'
>>> a
'some textmore stuff'
>>> b = r'some stuff'\
    ' even more'
>>> b
'some stuff even more'
>>> c = r'one' r'two'
>>> c
'onetwo'
>>> 

And now the part that fails me:

PCPath1 = r'Z:\IRF\Data\Primary Care Hospitals\PI\_'\
          '2008-11 (final)\2012.08.15 - 2008-11_PCH_v4.mdb'
PCConn1 = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)}; DBQ='+PCPath1)

I've got following error:

Traceback (most recent call last):
  File "Z:/IRF/Python/Production/S3_PC1_0811.py", line 7, in <module>
    PCConn1 = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)}; DBQ='+PCPath1)
UnicodeDecodeError: 'ascii' codec can't decode byte 0x81 in position 100: ordinal not in range(128)

It works OK when PCPath1 is not broken down.

One could ask why I'm trying to do it, well mostly is for the code readibility.

Any help with above would be greatly appreciated!

Was it helpful?

Solution

You need to put an r in front of the second line as well, otherwise the \ will be combined with the 201 to produce the non-ascii character \x81.

In [5]: r'Z:\IRF\Data\Primary Care Hospitals\PI\_'\
          '2008-11 (final)\2012.08.15 - 2008-11_PCH_v4.mdb'
Out[5]: 'Z:\\IRF\\Data\\Primary Care Hospitals\\PI\\_2008-11 (final)\x812.08.15 - 2008-11_PCH_v4.mdb'

In [6]: r'Z:\IRF\Data\Primary Care Hospitals\PI\_'\
          r'2008-11 (final)\2012.08.15 - 2008-11_PCH_v4.mdb'
Out[6]: 'Z:\\IRF\\Data\\Primary Care Hospitals\\PI\\_2008-11 (final)\\2012.08.15 - 2008-11_PCH_v4.mdb'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top