亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Python 搜索和替換 excel (.xls) 文件中的字符串?

如何使用 Python 搜索和替換 excel (.xls) 文件中的字符串?

LEATH 2023-05-09 10:56:34
我正在嘗試在 excel 文件 (xls) 中使用搜索和替換字符串,實際上 - 我需要查找字符串并在前綴 * 之前添加。例如但我收到一個錯誤:LVdemact = {'two': two_replaced, 'six': six_replaced, 'five': five_replaced}NameError: name 'two_replaced' is not defined代碼:from xlrd import open_workbookfrom xlutils.copy import copyimport xlsxwriterrb = open_workbook("template.xlsx")# Create an new Excel file and add a worksheet.wb = xlsxwriter.Workbook('Updated_Workbook1.xlsx')ws = wb.add_worksheet()s_orig = rb.sheet_by_index(0)LVdemact = {'two': two_replaced, 'six': six_replaced, 'five': five_replaced,}for row in range(s_orig.nrows):    for col in range(s_orig.ncols):        if s_orig.cell(row,col).value in LVdemact:            # s.write(row, col, LVdemact[item])            ws.write(row, col, LVdemact[s_orig.cell(row,col).value])        else:            ws.write(row, col, s_orig.cell(row,col).value)wb.close()
查看完整描述

2 回答

?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

問題在于 -LVdemact = {'two': two_replaced, 'six': six_replaced, 'five': five_replaced,}

變量two_replacedsix_replaced并且five_replaced未定義。你想讓它成為一個字符串嗎?然后以這種方式定義它 -

LVdemact = {'two': 'two_replaced', 'six': 'six_replaced', 'five': 'five_replaced'}


查看完整回答
反對 回復 2023-05-09
?
慕桂英3389331

TA貢獻2036條經驗 獲得超8個贊

你可以用這個


df = pd.DataFrame() # Your dataframe


for column in df.select_dtypes('object'):

    df[column] = df[column].str.replace('toreplace','newword')


查看完整回答
反對 回復 2023-05-09
  • 2 回答
  • 0 關注
  • 257 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號