-1

1.5 month with Python. I am using Python 3.3.2 with WinPython http://code.google.com/p/winpython/ and port xlwt to Python 3 from here https://github.com/hansrwl/xlwt/tree/py3 This is dict with values as lists. It writes to xls file correctly with the following function.

sampleData = {'Books': ['Book_A', 'Book_B', 'Book_C'],
              'Author': ['Author_A', 'Author_B', 'Author_C'],
              'Price': ['Price_A', 'Price_B', 'Price_C']}

function:

def saveDataToNewFile(fileName, sheetName, data):
    # Creating new workbook
    wb = xlwt.Workbook()
    # Creating new worksheet with the name specified
    ws = wb.add_sheet(sheetName)
    # Use dictionary keys as first row values(e.g. headers)
    for colIdx, headerCaption in enumerate(data):
        ws.write(0, colIdx, headerCaption)
        # Use dict values as row values for corresponding columns
        for rowIdx, itemVal in enumerate(data[headerCaption]):
            ws.write(rowIdx + 1, colIdx, itemVal)
    wb.save(fileName)

saveDataToNewFile('sample.xls', 'FirstSaveToXlsSample', sampleData)

- this saved correctly and opened with MS Excel.

I have the same data structure which is produced by this loop:

soup3 = defaultdict(list)
def init_fields(links_first_lvl):
    for link in links_first_lvl[1:7]:
soup3['Дата'].append(BeautifulSoup(urllib.request.urlopen(link).read()).select('.author_data'))
            soup3['Адрес'].append(link)
        return soup3

Here is the structure, dictionary with lists as values (i use pprint to print in console beauty)

PPRINT:
{'url': [  'http://www.ros.ru/article.php?chapter=1&id=20132503',
           'http://www.ros.ru/article.php?chapter=1&id=20132411'],
 'date': [[<div class="author_data"><b>Марта Моисеева
</b> № 30 (973) от 24.07.2013
<span class="rubr"> ВЛАСТЬ
</span></div>],
          [<div class="author_data"><b>Ольга Космынина
</b> № 29 (972) от 17.07.2013
<span class="rubr"> ВЛАСТЬ
</span></div>]]

saveDataToNewFile('sample2.xls', 'FirstSaveToXlsSample', soup3)

The problem: if i try to save to xls i get an error:

.....

if isinstance(data, basestring):
NameError: global name 'basestring' is not defined

Edit: this is full error stack in console Pycharm

Traceback (most recent call last):
  File "F:/Python/NLTK packages/parse_html_py3.3.2.py", line 91, in <module>
    saveDataToNewFile('sample2.xls', 'FirstSaveToXlsSample', soup3)
  File "F:/Python/NLTK packages/parse_html_py3.3.2.py", line 87, in saveDataToNewFile
    ws.write(rowIdx + 1, colIdx, itemVal)
  File "F:\WinPython-32bit-3.3.2.0\python-3.3.2\lib\site-packages\xlwt\Worksheet.py", line 1032, in write
    self.row(r).write(c, label, style)
  File "F:\WinPython-32bit-3.3.2.0\python-3.3.2\lib\site-packages\xlwt\Row.py", line 259, in write
    self.__rich_text_helper(col, label, style, style_index)
  File "F:\WinPython-32bit-3.3.2.0\python-3.3.2\lib\site-packages\xlwt\Row.py", line 276, in __rich_text_helper
    if isinstance(data, basestring):
NameError: global name 'basestring' is not defined

I don't know why, it should work because the structure is the same.

Vic Nicethemer
  • 1,081
  • 3
  • 16
  • 38
  • 2
    Seems like you are using something that is python2 only. `basestring` doesn't exist in python3. – Bakuriu Aug 07 '13 at 08:03

5 Answers5

0

The library you're using is written to work on python 2.x only. Download latest python 2.x from here and try again.

nosklo
  • 217,122
  • 57
  • 293
  • 297
0

Much lower overhead with LibreOffice. Open the LibreOffice install folder, and scrounge your UNO references from all of the current examples there. Save as XLS. Done.

ksed
  • 335
  • 1
  • 4
  • 13
0

As the comments suggest, the problem is you are using code that is still written for Python 2, however, before you downgrade to Python 2 as nosklo suggests, make sure you installed the xlwt from the Python 3 branch. After you cloned the xlwt repository, did you remember to perform

git checkout -b py3 origin/py3

before you performed your installation?

If you remembered and you still get the basestring error, then the py3 branch is still incomplete, and you will need to downgrade to Python 2 to run the code in master branch.

gotgenes
  • 38,661
  • 28
  • 100
  • 128
0

You may try changing 'basestring' to 'str'

0
if isinstance(data, basestring):
    NameError: global name 'basestring' is not defined

Probably the object has not such attribute and your test fails, since it just test if the object type is an instance or not, and it implies that your attribute or object already exists.

I recommend you to perform a prior test to verify if the attribute or object exists such as hasattr() or if you consider to use self you can read self.__dict__ to find the existing attributes.

Leo Pepe
  • 319
  • 2
  • 5