0

我有一个 .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文件的读取/读取不是很感兴趣,所以我想知道是否有人可以帮我找到那条线,获取这些值并将它们放在一个数组中。提前致谢。

4

2 回答 2

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
于 2013-08-14T11:19:02.620 回答
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”行,我们可以调整此代码以保存所有这些。

于 2013-08-14T11:24:29.190 回答