简体   繁体   English

Python 将 xlsx 读取为 Csv

[英]Python read xlsx as Csv

I'm using xlrd to read a xlsx file as a csv.我正在使用 xlrd 将 xlsx 文件作为 csv 读取。 For this purpose I'm using the following code:为此,我使用以下代码:

workbook = xlrd.open_workbook("170519_taxonomy_in_qiime.xlsx")
sheet = workbook.sheet_by_index(0)
source_data = [sheet.row_values(rowx) for rowx in range(sheet.nrows)]

Which gives me this example result这给了我这个示例结果

[[225145.0, 'k__Bacteria', ' p__ZB3', ' c__Rs-J96', ' o__', ' f__', ' g__', ' s__'], [2916972.0, 'k__Bacteria', ' p__ZB3', ' c__Rs-J96', 'o__', ' f__', ' g__', ' s__']]

But I need my result to look like this:但我需要我的结果是这样的:

[['225145.0, k__Bacteria,  p__ZB3,  c__Rs-J96,  o__,  f__,  g__,  s__'], ['2916972.0, k__Bacteria,  p__ZB3,  c__Rs-J96,  o__,  f__,  g__,  s__']]

Any Idea how can do this?任何想法如何做到这一点?

Each item in source_data is a list of values. source_data 中的每一项都是一个值列表。 You are trying to produce a single string containing each value in the list.您正在尝试生成包含列表中每个值的单个字符串。

You can use the str.join function for this.您可以为此使用str.join函数。 However, note that the first element in the list is a float value and not a string, so you first need to convert that to a string before using the join function.但是,请注意列表中的第一个元素是浮点值而不是字符串,因此您首先需要在使用 join 函数之前将其转换为字符串。

For example:例如:

source_data = [' '.join(map(str,sheet.row_values(rowx))) for rowx in range(sheet.nrows)]
x = [[225145.0, 'k__Bacteria', ' p__ZB3', ' c__Rs-J96', ' o__', ' f__', ' g__', ' s__'], [2916972.0, 'k__Bacteria', ' p__ZB3', ' c__Rs-J96', 'o__', ' f__', ' g__', ' s__']]

y = []
for c in x:
    b = ""
    for a in c:
        b =b + "".join(str(a))
    y.append(b)
y

Output输出

['225145.0k__Bacteria p__ZB3 c__Rs-J96 o__ f__ g__ s__',
 '2916972.0k__Bacteria p__ZB3 c__Rs-J96o__ f__ g__ s__']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM