如何將 word_for_replace 鏈接到 firstword 的值。有人能幫助我嗎 ?基本上我需要替換 test.xlsx 中第一個字的值我還需要稍后為第二個字的值呼叫這個函式(例如)
def replace_word():
word_for_replace =
i = 0
for r in range(1, sheet.max_row 1):
for c in range(1, sheet.max_column 1):
s = sheet.cell(r, c).value
if s != None and word_for_replace in s:
sheet.cell(r, c).value = s.replace(word_for_replace, "ZZZZZZZZZZZZ")
print("row {} col {} : {}".format(r, c, s))
i = 1
data.save('C:\\Users\\ancoman\\Desktop\\testX.xlsx')
print("{} cells updated".format(i))
def find_freq():
for cell in range(1, 3000):
data = sheet.cell(row=cell, column=2).value
if data is not None:
data=data.lower()
data_no_pct = data.translate(str.maketrans('', '', string.punctuation))
big_data.append(data_no_pct)
x = " ".join(big_data)
split_it = x.split()
Count1 = Counter(split_it)
most_occur1 = Count1.most_common(20)
print(most_occur1)
find_freq()
firstword = input('Please type word from list: ')
word_list_for_replace.append(firstword)
print('FirstWord is: ' firstword)
replace_word()
uj5u.com熱心網友回復:
您只需將其定義word_for_replace為函式的引數。firstword然后你可以在呼叫函式時傳遞任何變數(這里)。
它應該如下所示:
def replace_word(word_for_replace):
i = 0
for r in range(1, sheet.max_row 1):
for c in range(1, sheet.max_column 1):
s = sheet.cell(r, c).value
if s != None and word_for_replace in s:
sheet.cell(r, c).value = s.replace(word_for_replace, "ZZZZZZZZZZZZ")
[...]
find_freq()
firstword = input('Please type word from list: ')
word_list_for_replace.append(firstword)
print('FirstWord is: ' firstword)
replace_word(firstword)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/437991.html
標籤:Python python-3.x python-2.7 打开xlsx
