Find and replace strings in Excel (.xlsx) using Python

后端 未结 4 1416
没有蜡笔的小新
没有蜡笔的小新 2021-01-07 02:00

I am trying to replace a bunch of strings in an .xlsx sheet (~70k rows, 38 columns). I have a list of the strings to be searched and replaced in a file, formatted as below:-

4条回答
  •  忘掉有多难
    2021-01-07 02:47

    For reading and writing xls with Python, use xlrd and xlwt, see http://www.python-excel.org/

    A simple xlrd example:

    from xlrd import open_workbook
    wb = open_workbook('simple.xls')
    
    for s in wb.sheets():
        print 'Sheet:',s.name
        for row in range(s.nrows):
            values = []
            for col in range(s.ncols):
                print(s.cell(row,col).value)
    

    and for replacing target text, use a dict

    replace = {
        'bird produk': 'bird product',
        'pig': 'pork',
        'ayam': 'chicken'
        ...
        'kuda': 'horse'
    }
    

    Dict will give you O(1)(most of the time, if keys don't collide) time complexity when checking membership using 'text' in replace. there's no way to get better performance than that.

    Since I don't know what your bunch of strings look like, this answer may be inaccurate or incomplete.

提交回复
热议问题