简体   繁体   English

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

[英]How to search and replace string in excel (.xls) file using Python?

i am trying to use search and replace string in excel file (xls), actually - i need to find string and add before prefix *.我正在尝试在 excel 文件(xls)中使用搜索和替换字符串,实际上 - 我需要在前缀 * 之前找到字符串并添加。 For example例如

1

But i am getting a error:但我收到一个错误:

LVdemact = {'two': two_replaced, 'six': six_replaced, 'five': five_replaced}
NameError: name 'two_replaced' is not defined

Code:代码:

from xlrd import open_workbook
from xlutils.copy import copy
import xlsxwriter

rb = 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()

The issue is in the line - LVdemact = {'two': two_replaced, 'six': six_replaced, 'five': five_replaced,}问题出在一行 - LVdemact = {'two': two_replaced, 'six': six_replaced, 'five': five_replaced,}

variables two_replaced , six_replaced and five_replaced are not defined.变量two_replacedsix_replacedfive_replaced未定义。 Do you want this to be a string?你希望这是一个字符串吗? then define this in this way -然后以这种方式定义它 -

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

You can use this你可以用这个

df = pd.DataFrame() # Your dataframe

for column in df.select_dtypes('object'):
    df[column] = df[column].str.replace('toreplace','newword')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Python将XML文件导入Excel XLS文件模板? - How to import an XML file into an Excel XLS file template using Python? 如何使用python搜索和替换DOTM文件中的字符串 - How to search and replace string in DOTM file using python 如何使用python在文件中搜索字符串(比如str1)并将其替换为另一个字符串(比如str2)? - How to search a string(say str1) and replace it with another string(say str2) in a file using python? 使用python将列表中的元素搜索并替换为字符串,并将其替换为新文件 - search & replace elements of a list into a string iteratively into a new file using python 如何使用python更新xls文件? - How to Update xls file using python? 在python中读取.xls文件(使用pandas read_excel) - Reading an .xls file in python (using pandas read_excel) 如何在 Linux 中使用 python 打开/读取受密码保护的 xls 或 xlsx (Excel) 文件? - How to open / read password protected xls or xlsx (Excel) file using python in Linux? 如何在文件中搜索字符串并将其替换为Python中的多行? - How do I search a file for a string and replace it with multiple lines in Python? 使用python将Excel选项卡(工作表)从单个xls文件复制到单个目标xls文件 - Copy Excel tabs (worksheets) from individual xls files to single target xls file using python 使用 python pandas 将 json 字符串写入 xls 文件 - writing json string to xls file using python pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM