Extract columns from Excel using Python

泄露秘密 提交于 2019-11-29 08:06:55

The u means it is a unicode string, it gets put there when you call str(). If you write the string out to a file it wont be there. What you are getting is 1 row from the column. It's because you are using end_rowx=1 it returns a list with one element.

Try getting the column value lists:

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)

and then you can zip them into tuple lists:

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

Which are in the form:

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

If you want to print the pairs:

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

col_values returns a list of column values, if you want single values you can call sh.cell_value(rowx, cellx).

import xlrd

sh = xlrd.open_workbook('input.xls').sheet_by_index(0)
english = open("english.txt", 'w')
spanish = open("spanish.txt", 'w')
french = open("french.txt", 'w')
try:
    for rownum in range(sh.nrows):
        english.write(str(rownum)+ " = " +str(sh.cell(rownum, 0).value)+"\n")
        spanish.write(str(rownum)+ " = " +str(sh.cell(rownum, 1).value)+"\n")
        french.write(str(rownum)+ " = " +str(sh.cell(rownum, 2).value)+"\n")
finally:
    english.close()
    spanish.close()
    french.close()

Use pandas:

In [1]: import pandas as pd

In [2]: df = pd.ExcelFile('test.xls').parse('Sheet1', index_col=0) # reads file

In [3]: df.index = df.index.map(int)

In [4]: for col in df.columns:
   ...:     column = df[col]
   ...:     column.to_csv(column.name, sep='=')  # writes each column to a file                                                    
   ...:                                          # with filename == column name

In [5]: !cat English  # English file content
1=Hello
2=Hi
3=Bus
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!