Python,为列中的特定字符串搜索excel表,并将这些行提取到文本文件中

时间:2022-09-13 08:35:24

Here's the code I've frankensteined together from other posts,

这是我从其他文章中弄出来的代码,

import xlrd
import os.path
wb = xlrd.open_workbook(os.path.join('D:\Data','SPS1 demo data.xlsx'))
wb.sheet_names()
sh = #?
Strings=#variables
i = 1
file = open("Output.txt", "w")
while sh.cell(i,3).value = (Strings):
   file.write(#row)
   i = i + 1
file.close

It's not complete, but what I'm trying to accomplish is a search in column 3(or entire sheet, doesn't matter) for 5 specific strings and output those rows line by line to a text file, if possible csv formatted i.e. commas between each value.

它还不完整,但是我要做的是在第3列(或整个表)中搜索5个特定的字符串,并将这些行逐行输出到一个文本文件中,如果可能的话,在每个值之间格式化为csv,即逗号。

How can I set a variable to 5 possible strings? Would this need to be an array?
I think the way that I have it written here will overwrite the text file each time rather than append it, is that correct? And if so what's the correct function, "file.append(#stuff)"?

如何将变量设置为5个可能的字符串?这需要是一个数组吗?我想我写在这里的方式每次都会覆盖文本文件而不是附加它,对吗?如果是的话,正确的函数是什么?“file.append(#stuff)”?

1 个解决方案

#1


1  

This should work. You can't assign 5 strings to a single variable, without using a list or some other data type. You can however check to see if the third cell's value (i[2] - here) is equal to any of the strings you're looking for ("string1" - "string5" - here).

这应该工作。如果不使用列表或其他数据类型,就不能为单个变量分配5个字符串。但是,您可以检查第三个单元格的值(i[2] -这里)是否等于您正在查找的任何字符串(“string1”-“string5”)。

import xlrd
sheet_data = []   
wb = xlrd.open_workbook(Path_to_xlsx)
p = wb.sheet_names()
for y in p:
   sh = wb.sheet_by_name(y)
   for rownum in xrange(sh.nrows):
      sheet_data.append((sh.row_values(rownum)))

found_list = []
rows_to_be_saved = []
for i in sheet_data:
  if i[2] == "string1" or i[2] == "string2" or i[2] == "string3" or i[2] == "string4" or i[2] == "string5":
    found_list.append(i)
  else:
      rows_to_be_saved.append(i)

text_file = open("Output.txt", "w")
text_file.write(found_list)
text_file.close()

Your output written to the text file "Output.txt" will be comma separated as the rows in your excel are read into python as tuples in a list.

将输出写入文本文件“输出”。txt“将以逗号分隔,因为您的excel中的行被读入python作为列表中的元组。

#1


1  

This should work. You can't assign 5 strings to a single variable, without using a list or some other data type. You can however check to see if the third cell's value (i[2] - here) is equal to any of the strings you're looking for ("string1" - "string5" - here).

这应该工作。如果不使用列表或其他数据类型,就不能为单个变量分配5个字符串。但是,您可以检查第三个单元格的值(i[2] -这里)是否等于您正在查找的任何字符串(“string1”-“string5”)。

import xlrd
sheet_data = []   
wb = xlrd.open_workbook(Path_to_xlsx)
p = wb.sheet_names()
for y in p:
   sh = wb.sheet_by_name(y)
   for rownum in xrange(sh.nrows):
      sheet_data.append((sh.row_values(rownum)))

found_list = []
rows_to_be_saved = []
for i in sheet_data:
  if i[2] == "string1" or i[2] == "string2" or i[2] == "string3" or i[2] == "string4" or i[2] == "string5":
    found_list.append(i)
  else:
      rows_to_be_saved.append(i)

text_file = open("Output.txt", "w")
text_file.write(found_list)
text_file.close()

Your output written to the text file "Output.txt" will be comma separated as the rows in your excel are read into python as tuples in a list.

将输出写入文本文件“输出”。txt“将以逗号分隔,因为您的excel中的行被读入python作为列表中的元组。