小编典典

使用Python从Excel中提取列

python

我有一个带有ff:行/列结构的Excel文件

ID   English   Spanish   French
 1   Hello     Hilo      Halu
 2   Hi        Hye       Ghi
 3   Bus       Buzz      Bas

我想阅读Excel文件,提取row和col值,并
根据英语,西班牙语和法语列创建3个新文件。

所以我会有类似的东西:

英文文件:

"1" = "Hello"
"2" = "Hi"
"3" = "Bus"

我一直在使用xlrd。我可以打开,阅读和打印文件的内容。
但是,这是我使用此命令得到的(Excel文件已经
打开):

for index in xrange(0,2):
    theWord = '\n' + str(sh.col_values(index, start_rowx=index, end_rowx=1)) + '=' + str(sh.col_values(index+1, start_rowx=index, end_rowx = 1))
    print theWord

OUTPUT:

[u'Parameter/Variable/Key/String']=[u'ENGLISH'] <-- is this a list?, didn't the str() use to strip it out?

什么是ü做什么呢?如何移除方括号?


阅读 590

收藏
2021-01-20

共1个答案

小编典典

u意味着它是一个unicode字符串,当你把它从里面str()
如果您将字符串写到文件中,它将不会在那里。您得到的
是该列中的1行。这是因为使用end_rowx=1它会返回一个
包含一个元素的列表。

尝试获取列值列表:

ids = sh.col_values(0, start_rowx=1)
english = sh.col_values(1, start_rowx=1)
spanish = sh.col_values(2, start_rowx=1)
french = sh.col_values(3, start_rowx=1)

然后可以将zip它们放入元组列表:

english_with_IDS = zip(ids, english)
spanish_with_IDS = zip(ids, spanish)
french_with_IDS = zip(ids, french)

形式如下:

("1", "Hello"),("2", "Hi"), ("3", "Bus")

如果要打印对:

for id, word in english_with_IDS:
       print id + "=" + word

col_values返回列值的列表,如果需要单个值,
可以调用sh.cell_value(rowx, cellx)

2021-01-20