[英]Replace the text from an Excel Sheet in a File Using Python
I am very new in Python and I am trying to figure out how to write a code which searches a text file for a string in a cell in Excel sheet and replace that string with a consequtive cell from the same row.我是 Python 的新手,我正在尝试弄清楚如何编写代码,在文本文件中搜索 Excel 工作表中单元格中的字符串,然后用同一行中的后续单元格替换该字符串。
+-----------+------------+--------+
| A | B | C |
+-----------+------------+--------+
| a | a_1 | a_2 |
| b | b_1 | b_2 |
| c | c_1 | c_2 |
+-----------+------------+--------+
In the text_file it's written "This is a_xyz" .在 text_file 中写着"This is a_xyz" 。
What I try to do is, to create multiple text files like "This is a_1" and "This is a_2" and save them automatically.我尝试做的是创建多个文本文件,如“这是 a_1”和“这是 a_2”并自动保存它们。 Of course there will be more rows and columns.
当然会有更多的行和列。
Tricky thing for me is, the string is "a" in Excel sheet, but it needs to replace the whole "a_xyz" in which "a" is present.对我来说棘手的是,Excel 表中的字符串是“a” ,但它需要替换存在“a”的整个“a_xyz” 。
Any suggestions?有什么建议么? Thanks in advance.
提前致谢。
you'd probably want to use something like this library https://openpyxl.readthedocs.io/en/stable/ to open up the file.你可能想使用类似这个库https://openpyxl.readthedocs.io/en/stable/的东西来打开文件。 as far as I'm aware excel's format is not easily translated to text (try opening it in notepad and see for yourself:)).
据我所知,excel的格式不容易翻译成文本(尝试在记事本中打开它并亲自查看:))。 it's basically binary data.
它基本上是二进制数据。 Go and read the docs at the link I posted and you should be able to figure out how to iterate over each row.
Go 并阅读我发布的链接中的文档,您应该能够弄清楚如何遍历每一行。 as a first step try saving the exactly same excel file by going through them row by row.
作为第一步,尝试通过逐行浏览它们来保存完全相同的 excel 文件。 once you've done that, just edit the place where you're doing the save so that it swaps those columns.
完成后,只需编辑要保存的位置,以便交换这些列。 cheers
干杯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.