在文件中查找特定单词,获取行的内容,并将其保存在数组中

时间:2013-08-14 11:12:54

标签: python file

我有一个.xls文件,我转换为.csv,然后读取.csv,直到包含单词clientegen的一个特定行,获取该行并将其放在数组上。

到目前为止,这是我的代码:

import xlrd
import csv

def main():
    print "Converts xls to csv and reads csv"
    wb = xlrd.open_workbook('ejemplo.xls')
    sh = wb.sheet_by_name('Hoja1')
    archivo_csv = open('fichero_csv.csv', 'wb')
    wr = csv.writer(archivo_csv, quoting=csv.QUOTE_ALL)
    for rownum in xrange(sh.nrows):
        wr.writerow(sh.row_values(rownum))
    archivo_csv.close()

    f = open('fichero_csv.csv', 'r')
    for lines in f:
        print lines

if __name__ == '__main__':
    main()

这打印了我:

[... a lot of more stuff ...]

"marco 4","","","","","","","","","","","","","","",""

"","","","","","","","","","","","","","","",""

"","","","","","","","","","","","","","","",""

"clientegen","maier","embega","Jegan ","tapa pure","cil HUF","carcHUF","tecla NSS","M1 NSS","M2 nss","M3 nss","doble nss","tapon","sagola","clip volvo","pillar"

"pz/bast","33.0","40.0","34.0","26.0","80.0","88.0","18.0","16.0","8.0","6.0","34.0","252.0","6.0","28.0","20.0"

"bast/Barra","5.0","3.0","6.0","8.0","10.0","4.0","10.0","10.0","10.0","10.0","8.0","4.0","6.0","10.0","6.0"

[... a lot of more stuff ...]

我想要做的就是取clientegen行,并将行的内容保存在名为finalarray的新字符串数组上。

finalarray = ["maier", "embega", "Jegan", "tapa pure", "cil HUF", "carcHUF", "tecla NSS", "M1 NSS", "M2 nss", "M3 nss", "doble nss", "tapon", "sagola", "clip volvo", "pillar"]

我对python文件的读/读不是很多,所以我想知道如果有人能帮我找到那条线,获取这些值并将它们放在一个数组上。提前谢谢。

2 个答案:

答案 0 :(得分:0)

如果您只是在寻找包含clientegen的行,那么您可以尝试:

finalarray = list()
with open("fichero_csv.csv") as f:
  for line in f: #loop through all the lines
    words = line.split(" ") #get a list of all the words
    if "clientegen" in words: #check to see if your word is in the list
      finalarray = words #if so, put the word list in your finalarray
      break  #stop checking any further

答案 1 :(得分:0)

如果你为for循环交换这个for循环,它应该做的诀窍:

    for rownum in xrange(sh.nrows):
        row = sh.row_values(rownum)
        if row[0] == "clientegen":  # Check if "clientgen" is the first element of the row
            finalarray = list(row)  # If so, make a copy of it and name it `finalarray`
        wr.writerow(row)

如果有多个“clientegen”行,我们可以调整此代码以保存所有这些代码。