将行写入Excel文件

时间:2017-04-17 19:41:54

标签: python excel xlrd xlwt

目标是从具有8列和数千行的特定输入excel文件中提取特定数据。然后脚本将编写一个包含特定搜索字符串的行的新文件。例如,我对Tungsten(W)特别感兴趣,所以我的搜索字符串将是' W'用于用户输入。到目前为止,我能够打印包含W及其内容的所有行,但现在包含搜索字符串的行需要保存到自己的excel文件中。您可以看到已注释掉的行是我想象的我将实现此代码以及我认为它可能是什么样子。 如果读者可以提供一些建议或行,以便将excel行中的读取内容写入新的excel文件,那将极大地帮助我

    # Store the filename into a variable.
    filename = 'Tester.xlsx'

    user_input = input("Enter an element of interest: ")
    print("The element of interest is '%s'.\n" % user_input)

    import xlwt #import to use xlwt to write to excel file
    workbook = xlwt.Workbook() # create a new workbook
    sheet1 = workbook.add_sheet("%sSheet" % user_input)

    counter = 0

    from xlrd import open_workbook
    book = open_workbook(filename)
    for sheet in book.sheets():
        for rowidx in range(sheet.nrows):
            row = sheet.row(rowidx)
            for colidx, cell in enumerate(row):
                if cell.value == user_input :
                    counter = counter + 1
                    print(colidx)
                    print(rowidx)
                    print(row)
    #                sheet1.write(counter,0,row)

    workbook.save("'%s'.xls" % user_input)

编辑:我尝试使用不同的方法,我只使用文本文件,但我想只在第一次尝试时使用excel文件达到相同的效果。

#Import Modules to read and write Excel Files
import xlwt
import xlrd

# Store the filename into a variable.
filename = 'Tester.txt'

# Open the file and read the lines into the data variable
f = open(filename)
data = f.readlines()
f.close()

# Print the lines in the terminal for visual confirmation
print("The strings in the file are as follows:\n")
for i in range(0,len(data)):
    print(data[i])

"""
Allow User to input a search string
Check to see if the search string appears in the file
If the String appears, list the line number of each occurrence. If not, 
print not found.
"""

user_input = input("Enter a search string: ")
print("The search string is '%s'.\n" % user_input)


"""
Print the Header
"""
#with open(filename) as f1:
#    with open('OutputTest.txt', 'a') as f2:  
#        headline = f1.readlines()
#        for i, line in enumerate(headline):
#            f2.write(line[0])


with open(filename) as f1:
    with open('OutputTest.txt', 'a') as f2:
        lines = f1.readlines()
        for i, line in enumerate(lines):
            if user_input in line:
                f2.write(line)


g = open('OutputTest.txt', 'r+')
row_list = []
for row in g:
    row_list.append(row.split())
column_list = zip(*row_list)
workbook_convert = xlwt.Workbook()
worksheet_convert = workbook_convert.add_sheet('ConvertedSheet1')
i = 0 
for column in column_list:
    for item in range(len(column)):
         worksheet_convert.write(item, i, column[item])
    workbook_convert.save('Excel_Convert_Test.xls')
    i+=1

print("All of the %s data has been collected. Please see the generated 
files." % user_input)

1 个答案:

答案 0 :(得分:0)

我确信有更有效的方法来完成您的任务,但是我有锤子,因此您的问题对我来说就像钉子。

我个人会将输入的excel文件读入pandas数据框。从那里,我可以用所有的熊猫优点进行所有想要的过滤。最后,我可以根据需要将结果写回到同一文件,新文件,csv或数据库中的新工作表中。

也许熊猫会带来很多开销,但这听起来像是一项任务,需要一个健壮的解决方案,如果需求发生变化,则无需重写。