엑셀

2014-03-25 2 views
0

내가 좋아하는 사전 저장하려고 파일로 사전을 저장하는 방법 : Excel 파일로엑셀

Allfiles={'woof': '0', 'jpg': '0', 'js': '45', 'gif': '0', 'css': '11', 'png': '6'} 

합니다. 나는 Excel을 검사 할 때

workbook=xlwt.Workbook(encoding='ascii') 
sheet1=workbook.add_sheet('Parsed data') 
for col, caption in enumerate(Allfiles): 
    sheet1.write(0,col,caption) 
    for row,item in enumerate(Allfiles[caption]): 
     sheet1.write(row+1,col,str(item)) 
workbook.save('savexl.xls') 

출력이 조롱이다 :

그리고 내 코드는 같다. 죄송합니다 나의 명성은 사진을 게시하기에 충분하지 않지만, 문제는 프로그램이 정수 '11'과 '45'를 문자열로 처리하여 다른 셀에 넣는 것입니다. 그러나 그 값을 문자열로 설정하지 않으면 "int is iterable"오류가 발생합니다. 그래서 어떤 사람이 나를 도와 줄 수 있니?


업데이트 : 새 사전 같은 :

Alllists={'scrip': ['10.183.195.140'], 'host': ['edigitalsurvey.com', 'ichef.bbci.co.uk',  'notify3.dropbox.com', 'sa.bbc.co.uk', 'static.bbci.co.uk', 'www.bbc.co.uk'], 'dstip': ['108.160.162.38', '212.58.244.69', '46.236.9.36', '77.72.112.168', '81.23.53.170', '81.23.53.171'], 'referer': ['http://static.bbci.co.uk/frameworks/barlesque/2.60.6/orb/4/style/orb-fixed.css', 'http://static.bbci.co.uk/h4discoveryzone/0.233.1/style/h4discoveryzone.css', 'http://static.bbci.co.uk/h4drawers/0.66.1/style/h4drawers.css', 'http://static.bbci.co.uk/h4more/0.114.2/style/h4more.css', 'http://static.bbci.co.uk/h4popular/0.130.1/style/h4popular.css', 'http://static.bbci.co.uk/h4whatson/0.176.5/style/h4whatson.css', 'http://www.bbc.co.uk/'], 'server': []} 

내가 코드를 사용합니다

for col,caption in enumerate(Alllists): 
    sheet1.write(4,col,caption) 
    for row,item in enumerate(Alllists[caption]): 
     sheet1.write(row+1,col,item) 
workbook.save('savexl.xls') 

BTW, 새로운 사전 Excel 파일에 다른 2에서 저장해야 . 난 다시 추적을 가지고

는 말했다 :

sheet1.write(row+1,col,item) 
Exception: Attempt to overwrite cell: sheetname=u'Parsed data' rowx=1 colx=0 

누구나 생각이있어?

+0

예상되는 출력 형식은 무엇입니까? 행 또는 열의 키? – dorvak

+0

"하지만 그 값을 문자열로 설정하지 않았다면"css ": '11'을했을 것입니다. 어떻게 사전을 만들었습니까? 언제 당신은 int를 반복 할 수 없습니까? – fredtantini

답변

1

사전에 행 목록 대신 행이 하나만 있습니다.

Allfiles={'woof': [0], 'jpg': [0], 'js': [45], 'gif': [0], 'css': [11], 'png': [6]} 

또는처럼 뭔가에 루프를 변경 : 당신은 그런 당신의 사전 구조를 변경할 수 있습니다

workbook=xlwt.Workbook(encoding='ascii') 
sheet1=workbook.add_sheet('Parsed data') 
for col, caption in enumerate(Allfiles): 
    sheet1.write(0,col,caption) 
    sheet1.write(1,col,Allfiles[caption]) 
workbook.save('savexl.xls') 

원래 사전 작업을 할 수 있습니다.

파이썬의 문자열이 문자 목록으로 계산되어 [11]을 [ '1', '1']로 사용할 수 있기 때문에 코드에서 예외가 발생하지 않는 유일한 이유가 있습니다.

+0

그게 도움이됩니다, 고마워요! – xpnhcnsh

+0

안녕하세요 새로운 문제가 있습니다. 업데이트를 확인하고 가도 될까요? – xpnhcnsh

+0

자막은 네 번째 줄로 이동합니다. 항목 중 하나가 캡션을 덮어 씁니다. 동일한 셀에 두 번 쓸 때 xlwt는 기본적으로 예외를 throw합니다. 아마도 첫 번째 행 ('sheet1.write (0, col, caption)')에 캡션을 써야하거나 4 이하의 행에 항목을 써야 할 것입니다 (예를 들어'for row, 열거 항목 (Alllists [caption], 4)'는 열거를 시작합니다 4) – x3al