파이썬으로 1.5 개월. 나는 파이썬 3.3.2를 WinPython과 함께 사용하고있다. http://code.google.com/p/winpython/ 그리고 여기에서 파이썬 3에 포트 xlwt을 연결한다. https://github.com/hansrwl/xlwt/tree/py3 이것은 값을리스트로 가지고있다. 다음 함수를 사용하여 xls 파일에 올바르게 기록됩니다.파이썬 xls에 목록 사전 쓰기
sampleData = {'Books': ['Book_A', 'Book_B', 'Book_C'],
'Author': ['Author_A', 'Author_B', 'Author_C'],
'Price': ['Price_A', 'Price_B', 'Price_C']}
기능 :
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)
-이 올바르게 저장 및 MS Excel을 열었다. 여기
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
값으로서리스트와 구조, 사전 (I 콘솔 아름다움 인쇄 pprint 사용)
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)
이다 :
난이 루프에 의해 생성되는 것과 동일한 데이터 구조를 갖는다
문제 : 내가 XLS로 저장하려고하면 나는 오류 얻을 :
을 .....
if isinstance(data, basestring):
NameError: global name 'basestring' is not defined
편집 :이 콘솔 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
나는 구조가 동일하기 때문에 작동해야하는 이유를 모르겠어요에서 전체 오류 스택이다.
어디에서이 오류가 발생합니까? xls 라이브러리에 있습니까? – Nadh
위의 편집 포스트에 추가되었습니다. –
여러분은 python2 만 사용하고있는 것처럼 보입니다. 'basestring'은 python3에는 존재하지 않습니다. – Bakuriu