<legend id='57V5X'><style id='57V5X'><dir id='57V5X'><q id='57V5X'></q></dir></style></legend>

<i id='57V5X'><tr id='57V5X'><dt id='57V5X'><q id='57V5X'><span id='57V5X'><b id='57V5X'><form id='57V5X'><ins id='57V5X'></ins><ul id='57V5X'></ul><sub id='57V5X'></sub></form><legend id='57V5X'></legend><bdo id='57V5X'><pre id='57V5X'><center id='57V5X'></center></pre></bdo></b><th id='57V5X'></th></span></q></dt></tr></i><div id='57V5X'><tfoot id='57V5X'></tfoot><dl id='57V5X'><fieldset id='57V5X'></fieldset></dl></div>

    1. <small id='57V5X'></small><noframes id='57V5X'>

        <tfoot id='57V5X'></tfoot>

        • <bdo id='57V5X'></bdo><ul id='57V5X'></ul>

        使用 Python 在 Excel (.xlsx) 中查找和替换字符串

        时间:2023-08-30
      1. <small id='yhkhw'></small><noframes id='yhkhw'>

            <tbody id='yhkhw'></tbody>
          <tfoot id='yhkhw'></tfoot>

              • <bdo id='yhkhw'></bdo><ul id='yhkhw'></ul>
                <i id='yhkhw'><tr id='yhkhw'><dt id='yhkhw'><q id='yhkhw'><span id='yhkhw'><b id='yhkhw'><form id='yhkhw'><ins id='yhkhw'></ins><ul id='yhkhw'></ul><sub id='yhkhw'></sub></form><legend id='yhkhw'></legend><bdo id='yhkhw'><pre id='yhkhw'><center id='yhkhw'></center></pre></bdo></b><th id='yhkhw'></th></span></q></dt></tr></i><div id='yhkhw'><tfoot id='yhkhw'></tfoot><dl id='yhkhw'><fieldset id='yhkhw'></fieldset></dl></div>
                  <legend id='yhkhw'><style id='yhkhw'><dir id='yhkhw'><q id='yhkhw'></q></dir></style></legend>
                  本文介绍了使用 Python 在 Excel (.xlsx) 中查找和替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试替换 .xlsx 工作表中的一堆字符串(约 70k 行,38 列).我有一个要在文件中搜索和替换的字符串列表,格式如下:-

                  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:-

                  bird produk - bird product
                  pig - pork
                  ayam - chicken
                  ...
                  kuda - horse
                  

                  要搜索的词在左边,替换词在右边(找到'bird produk',替换为'bird product'.我的.xlsx 表看起来像这样:-

                  The word to be searched is on the left, and the replacement is on the right (find 'bird produk', replace with 'bird product'. My .xlsx sheet looks something like this:-

                  name     type of animal     ID
                  ali      pig                3483
                  abu      kuda               3940
                  ahmad    bird produk        0399
                  ...
                  ahchong  pig                2311
                  

                  我正在为此寻找最快的解决方案,因为我在要搜索的列表中有大约 200 个单词,而且 .xlsx 文件非常大.我需要为此使用 Python,但我愿意接受任何其他更快的解决方案.

                  I am looking for the fastest solution for this, since I have around 200 words in the list to be searched, and the .xlsx file is quite large. I need to use Python for this, but I am open to any other faster solutions.

                  - 添加工作表示例

                  Edit2:- 尝试了一些 python 代码来读取单元格,花了很长时间来读取.有什么指点吗?

                  - tried some python codes to read the cells, took quite a long time to read. Any pointers?

                  from xlrd import open_workbook
                  wb = open_workbook('test.xlsx')
                  
                  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)
                  

                  谢谢!

                  Edit3:- 我终于想通了.VBA 模块和 Python 代码都可以工作.我改用 .csv 来让事情变得更容易.谢谢!这是我的 Python 代码版本:-

                  import csv
                  
                  ###### our dictionary with our key:values. ######
                  reps = {
                      'JUALAN (PRODUK SHJ)' : 'SALE( PRODUCT)',
                      'PAMERAN' : 'EXHIBITION',
                      'PEMBIAKAN' : 'BREEDING',
                      'UNGGAS' : 'POULTRY'}
                  
                  
                  def replace_all(text, dic):
                      for i, j in reps.items():
                          text = text.replace(i, j)
                      return text
                  
                  with open('test.csv','r') as f:
                      text=f.read()
                      text=replace_all(text,reps)
                  
                  with open('file2.csv','w') as w:
                      w.write(text)
                  

                  推荐答案

                  我会将文本文件的内容复制到 Excel 文件中的新工作表中,并将该工作表命名为查找".然后使用 text to columns 来获取这个新工作表的前两列中的数据,从第一行开始.

                  I would copy the contents of your text file into a new worksheet in the excel file and name that sheet "Lookup." Then use text to columns to get the data in the first two columns of this new sheet starting in the first row.

                  将以下代码粘贴到 Excel 中的模块中并运行它:

                  Paste the following code into a module in Excel and run it:

                  Sub Replacer()
                      Dim w1 As Worksheet
                      Dim w2 As Worksheet
                  
                      'The sheet with the words from the text file:
                      Set w1 = ThisWorkbook.Sheets("Lookup")
                      'The sheet with all of the data:
                      Set w2 = ThisWorkbook.Sheets("Data")
                  
                      For i = 1 To w1.Range("A1").CurrentRegion.Rows.Count
                          w2.Cells.Replace What:=w1.Cells(i, 1), Replacement:=w1.Cells(i, 2), LookAt:=xlPart, _
                          SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                          ReplaceFormat:=False
                      Next i
                  
                  End Sub
                  

                  这篇关于使用 Python 在 Excel (.xlsx) 中查找和替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:replaceWith() 后的 find() 不起作用(使用 BeautifulSoup) 下一篇:是否可以在 Pymongo 中创建一个没有光标超时的聚合?

                  相关文章

                    <small id='IMIeW'></small><noframes id='IMIeW'>

                    <tfoot id='IMIeW'></tfoot>
                      <legend id='IMIeW'><style id='IMIeW'><dir id='IMIeW'><q id='IMIeW'></q></dir></style></legend>
                        <bdo id='IMIeW'></bdo><ul id='IMIeW'></ul>
                    1. <i id='IMIeW'><tr id='IMIeW'><dt id='IMIeW'><q id='IMIeW'><span id='IMIeW'><b id='IMIeW'><form id='IMIeW'><ins id='IMIeW'></ins><ul id='IMIeW'></ul><sub id='IMIeW'></sub></form><legend id='IMIeW'></legend><bdo id='IMIeW'><pre id='IMIeW'><center id='IMIeW'></center></pre></bdo></b><th id='IMIeW'></th></span></q></dt></tr></i><div id='IMIeW'><tfoot id='IMIeW'></tfoot><dl id='IMIeW'><fieldset id='IMIeW'></fieldset></dl></div>