python数据读取(excel、csv、tsv)

python数据读取(excel、csv、tsv),第1张

excel读取
#读取列表P   必须把附件与本程序置于同一路径!
from openpyxl import *

wb=load_workbook('附件.xlsx')
sheet=wb.get_sheet_by_name('Sheet1')

def r_xlxs(x):#读取数据到列表P
    L=[]
    for i in sheet[x]:
        L.append(i.value)
    return L

B=r_xlxs('B')
C=r_xlxs('C')
#有多列的自己加..

P=[]
for i in range(1,len(B)-1):#从1开始是因为第0行一般是文字
    p=(B[i],C[i],i-1)
    P.append(p)
列表数据写入CSV
import csv #调用数据保存文件
import pandas as pd #用于数据输出
y1_j= [12.697433731817252, 29.20946279943478, 49.67323524323002, 102.88872277028304, 172.95152378588483, 260.14917353058166]
y2_j= [12.697434705656148, 29.209335842559813, 49.67162354953921, 102.8596739269148, 172.8617729038298, 260.0592364430679]
y3_j= [12.697444267517733, 29.208966696508188, 49.66134738870873, 102.71200463740381, 172.47114709328892, 259.72051120522076]
y4_j= [12.697523854073596, 29.2076660189428, 49.630150039544326, 102.33955478334542, 171.58165579048637, 258.97900442099444]
y5_j= [12.697391090950354, 29.196235034586824, 49.412658430997, 100.12739415822013, 166.30174161735337, 247.94405307296105]
y6_j= [12.701194603318868, 29.149956734930655, 48.655819938382166, 91.77462349516475, 139.48350457473015, 189.6119072414784]
y7_j= [13.901056630066016, 29.006123485565933, 46.542109857216175, 72.78566488101947, 83.5985050921565, 80.75520583184539]
list_jmax=[y1_j,y2_j,y3_j,y4_j,y5_j,y6_j,y7_j]
column=['0.1','0.2','0.3','0.5','0.7','0.9'] #列表头名称
test=pd.DataFrame(columns=column,data=list_jmax)
test.to_csv('D:/test.csv') #存储位置及文件名称
Pandas读取csv/tsv

data不是列表,第一行开始就是元素,不含列名

import pandas as pd

data = pd.read_table("fileName.csv",sep=",")  #tsv改成\t

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/873347.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存