0
當我運行下面的代碼一切正常發現:無法輸出保存到Excel工作簿時,Python中填充數據
wb7 = load_workbook('define2.xlsx')
ws = wb7.worksheets[0]
ws.cell(row=3,column=2).value = "hey"
wb7.save('define2.xlsx')
但是,當我把最後一行代碼在不同的代碼結束時,我得到一個錯誤信息。而且是正確生成該錯誤,因爲我跨過線:
wb7.save('define2.xlsx')
我得到的錯誤信息是
TypeError: %d format: a number is required, not tuple
我覺得奇怪,因爲我看不出爲什麼wb7必須是一個數。所有我想要做的是輸出一些數據到Excel然後保存Excel工作簿。我做了很多次,沒有任何問題。一定是這樣,wb7變量得到了改變,但顯然沒有改變。在調試器中,運行代碼後沒有發現wb7被更改的證據。我也得到我的控制檯如下:
/Library/Python/2.7/site-packages/openpyxl/reader/worksheet.py:322: UserWarning: Unknown extension is not supported and will be removed
warn(msg)
Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 2357, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1777, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Users/kylefoley/PycharmProjects/untitled3/temp5.py", line 1682, in <module>
wb7.save('define2.xlsx')
File "/Library/Python/2.7/site-packages/openpyxl/workbook/workbook.py", line 263, in save
save_workbook(self, filename)
File "/Library/Python/2.7/site-packages/openpyxl/writer/excel.py", line 239, in save_workbook
writer.save(filename, as_template=as_template)
File "/Library/Python/2.7/site-packages/openpyxl/writer/excel.py", line 222, in save
self.write_data(archive, as_template=as_template)
File "/Library/Python/2.7/site-packages/openpyxl/writer/excel.py", line 80, in write_data
self._write_worksheets(archive)
File "/Library/Python/2.7/site-packages/openpyxl/writer/excel.py", line 163, in _write_worksheets
xml = sheet._write(self.workbook.shared_strings)
File "/Library/Python/2.7/site-packages/openpyxl/worksheet/worksheet.py", line 778, in _write
return write_worksheet(self, shared_strings)
File "/Library/Python/2.7/site-packages/openpyxl/writer/worksheet.py", line 198, in write_worksheet
dim = Element('dimension', {'ref': '%s' % worksheet.calculate_dimension()})
File "/Library/Python/2.7/site-packages/openpyxl/worksheet/worksheet.py", line 420, in calculate_dimension
get_column_letter(max_col), max_row
下面是完整的代碼:
wb7 = load_workbook('define2.xlsx')
ws = wb7.worksheets[0]
temp_list = []
category = ['r','s','t']
relat = []
srelat = []
trelat = []
word_type = []
atomic_relations = []
list1 = []
list4 = []
det = []
adj = []
adv = []
noun = []
cor = []
lcon = []
subo = []
synon = []
redundant = []
aux = []
atomic_relata = []
negg = []
dnoun = []
det_pairs = []
definitions = []
syn_pairs = []
relations = []
relations2 = []
ind_words = []
ind_num = []
particles = []
osent = []
sn = 1
i = 0
for row in ws.rows:
i += 1
if i == 430:
pp = 5
str1 = row[0].value
str5 = copy.copy(str1)
str2 = row[1].value
if str1 != None and str2 != None:
str3 = row[2].value
defin = row[3].value
if str2 != None:
str2 = str2.strip()
if str3 != None:
str3 = str3.strip()
atom = copy.copy(str1)
str5 = str1[0:1]
atom = atom[1:2]
str8 = copy.copy(str1)
str8 = str1[3:]
if str5 in category:
word_type.append([str3,str5])
elif str5 != 'z':
word_type.append([str2, str5])
if str5 in category:
if str5 == 'r':
relat.append(str3)
elif str5 == 's':
srelat.append(str3)
elif str5 == 't':
trelat.append(str3)
if atom == 'a':
atomic_relations.append(str3)
atom = 'b'
list2a = [str2,str3]
relations.append(list2a)
relations2.append(str2)
elif str5 == 'a':
adj.append(str2)
elif str5 == 'b':
aux.append(str2)
elif str5 == 'c':
cor.append(str2)
elif str5 == 'd':
det.append(str2)
elif str5 == 'e':
adv.append(str2)
elif str5 == 'l':
lcon.append(str2)
elif str5 == 'm':
negg.append(str3)
elif str5 == 'n':
noun.append(str2)
elif str5 == 'u':
subo.append(str2)
if atom == 'n':
dnoun.append(str2)
elif atom == 'a':
atomic_relata.append(str2)
elif atom == 'q':
particles.append(str2)
elif atom == 'r':
redundant.append(str2)
if atom == 'p' or atom == 'd':
if atom == 'p':
atom = 7
elif atom == 'd':
atom = 5
list1a = [str2, atom]
ind_words.append(str2)
ind_num.append(list1a)
elif atom == 's':
str6 = defin[defin.find("=")+1:-1]
str6 = str6.strip()
str7 = defin[1:defin.find("=")]
str7 = str7.strip()
list3a = [str7, str6, defin]
syn_pairs.append(list3a)
synon.append(str7)
elif atom == 'n':
str6 = defin[defin.find(".")+1:]
str7 = defin[:defin.find(".")]
det_pairs.append([str7, str6, str2])
if atom != 'a':
if str5 in category:
definitions.append([str3, defin])
else:
definitions.append([str2, defin])
words = [adj, cor, det, adv, lcon, noun, relat, srelat, trelat, subo, aux, negg, dnoun]
word_type.sort()
syn_pairs.sort()
del word_type[0]
test_sent = []
j = 0
for i in ws.rows:
j += 1
if j > 263:
break
if j == 169:
pp = 7
str1 = i[0].value
str2 = i[1].value
if str1 != None:
str2 = str2.strip()
if str1[1:2] == 's' and j>139:
str3 = findinlist(str2,syn_pairs,0,1)
if str3 == None:
pp = 7
str4 = findinlist(str3, word_type,0,1)
if str4 == None:
pp = 7
ws.cell(row=i,column=1).value = str4 + 'sx'
wb7.save('define2.xlsx')
也許這裏的教訓是使用有意義的名字。 ;-) – BlackJack
我認爲你是對的。 – kylefoley76