将Excel行读入Python [英] Read Excel lines into Python
本文介绍了将Excel行读入Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要导入保存在Excel CSV中的数据行.如何将每一行的数据保存为列表?
I want to import the data lines saved in an Excel CSV. How can I save each line's data as a list?
Excel文件具有以下类型的数据:
The Excel file has the following type of data:
Name B C D
ItemID 3 3 4
Height 5 5 7
Length 6 5 8
我想将[B,C,D]保存在名为Name
的列表中,并将[3,3,4]保存为名为ItemID
的列表.我尝试使用以下代码,但它始终返回文件的最后一行.
I want to save [B,C,D] in a list named Name
, and [3,3,4] as a list named ItemID
. I tried to used the following code, but it always returns the last line of the file.
f = open('part.csv', 'r')
csv_f=csv.reader(f,delimiiter=',')
for row in csv_f:
name.append(row[1])
numid.append(row[2])
height.append(row[3])
length.append(row[4])
推荐答案
强烈建议考虑使用 pandas python数据分析库.
Highly recommend to consider using pandas the python data analysis library.
import pandas as pd
csv_df = pd.read_csv('part.csv')
df_T = csv_df.T
print df_T['ItemID'].tolist()
print df_T.index.tolist()
这篇关于将Excel行读入Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文