使用Python在excel中替换字符串 [英] String replacement in excel using Python

查看:607
本文介绍了使用Python在excel中替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除并替换为字符串,并删除空格并将其替换为分数。



这是我迄今为止所做的:

 #从用户打开Excel文件输入
import xlrd ,xlwt
filename = raw_input(输入Excel文件名,扩展名(.xls)和路径​​)
oldbook = xlrd.open_workbook(filename)
newbook = xlwt.Workbook()

#对于工作簿中的所有工作表
在oldbook.sheet_names()中的sheetname:
oldsheet = oldbook.sheet_by_name(sheetname)
newsheet = newbook.add_sheet(sheetname )

#对于范围(oldsheet.nrows)中的ii的所有行和excel
中的所有列:
for range(oldsheet.ncols)中的jj:
#替换
range.replace(%,Perc)

#使用所需的名称将文件保存在所需的位置
savelocation = raw_input(使用扩展名(.xls)输入新的路径和文件名,以保存新的Excel电子表格)
newbook.save(savelocation)


解决方案

一个建议,将单元格数据读入一个字符串,然后操作它。



尝试这样:(不幸的是我目前无法运行)

 #从用户打开Excel文件imput 
import xlrd,xlwt
filename = raw_input(输入Excel文件名与扩展名(.xls)和路径)
oldbook = xlrd.open_workbook(filename)
newbook = xlwt.Workbook()

#对于工作簿中的所有工作表
for oldbook.sheet_names()中的sheetname:
oldsheet = oldbook.sheet_by_name(sheetname)
newsheet = newbook.add_sheet(sheetname)

#对于所有行和全部在范围(oldsheet.nrows)中的ii中的excel
中的列:
for range(oldsheet.ncols)中的jj:
#替换
CellString = str(oldsheet.cell (ii,jj).Value)
CellString = CellString.replace(%,Perc)
CellString = CellString.replace(,_)
newsheet.write(ii,jj,CellString)
#将文件保存在所需位置,所需名称为
savelocation = raw_input(使用扩展名(.xls)输入新路径和文件名以保存新的Excel电子表格)
newbook.save(savelocation)
/ pre>

I want to remove % and replace with a string, and to remove space and replace it with under score.

This is what I have done so far:

# Open Excel file from a user imput
import xlrd, xlwt
filename = raw_input("Enter Excel file name with extension (.xls) and path")
oldbook = xlrd.open_workbook(filename)
newbook = xlwt.Workbook()

# For all the sheets in the workbook
for sheetname in oldbook.sheet_names():
    oldsheet = oldbook.sheet_by_name(sheetname)
    newsheet = newbook.add_sheet(sheetname)

    # For all the rows and all the columns in an excel
    for ii in range(oldsheet.nrows):
        for jj in range(oldsheet.ncols):
            # Replace
            range.replace("%", "Perc")

# Save the file in a desired location with the desired name
savelocation = raw_input("Enter a new path and file name with extension (.xls) to save the new Excel spread sheet ")
newbook.save(savelocation)

解决方案

One advice, read cell data into a string and then manipulate it.

Try this: (Unfortunately I cannot run it at the moment)

# Open Excel file from a user imput
import xlrd, xlwt
filename = raw_input("Enter Excel file name with extension (.xls) and path")
oldbook = xlrd.open_workbook(filename)
newbook = xlwt.Workbook()

# For all the sheets in the workbook
for sheetname in oldbook.sheet_names():
    oldsheet = oldbook.sheet_by_name(sheetname)
    newsheet = newbook.add_sheet(sheetname)

    # For all the rows and all the columns in an excel
    for ii in range(oldsheet.nrows):
        for jj in range(oldsheet.ncols):
            # Replace
            CellString=str(oldsheet.cell(ii, jj).Value)
            CellString=CellString.replace("%", "Perc")
            CellString=CellString.replace(" ", "_")
            newsheet.write(ii, jj, CellString)
# Save the file in a desired location with the desired name
savelocation = raw_input("Enter a new path and file name with extension (.xls) to save the new Excel spread sheet ")
newbook.save(savelocation)

这篇关于使用Python在excel中替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆