基礎操作技能 3之python跨檔案呼叫

2021-09-29 20:33:39 字數 2862 閱讀 5629

#讀取檔案

import pandas as pd

data = pd.

read_csv

("/data1/data2/data3/data/data.csv"

)#應用os路徑拼接os.path.

join

()函式

#函式的作用連線兩個或者更多的路徑元件

import os

data_path =

'path1'

path2 =

'develop'

path3 =

'data_list'

path = os.path.

join

(data_path, path2, path3)

#在應用的時候直接就可以應用path

#字串

num_str =

'12345678'

sorted

(num_str)

out:

['1'

,'2'

,'3'

,'4'

,'5'

,'6'

,'7'

,'8'

]#列表

num_vec =[1

,3,9

,5,3

]sorted

(num_vec)

out:[1

,3,3

,5,9

]#字典

(預設)

num_dic =

sorted

(num_dic)

out:[1

,2,3

]#對字典的鍵

sorted

(num_dic.

keys()

) out:[1

,2,3

]#對字典的值

sorted

(num_dic.

values()

) out:

['c'

,'g'

,'q'

]#對鍵值對組成的元組的列表

sorted

(num_dic.

items()

) out:[(

1,'q'),(

2,'g'),(

3,'c')

]#自定義key

#以中間數字為key排序

str_vec =

['a_1.csv'

,'c_5.csv'

,'e_10.csv'

,'p_3.csv'

,'h_7.csv'

]sported

(str_vec, key = lambda d:

int(d.

split

('_')[

-1].

split

('.')[

0]))

out:

['a_1.csv'

,'p_3.csv'

,'c_5.csv'

,'h_7.csv'

,'e_10.csv'

]#加入reverse

sorted

(str_vec, key = lambda d:

int(d.

split

('_')[

-1].

split

('.')[

0]),

reverse=true)

out:

['e_10.csv'

,'h_7.csv'

,'c_5.csv'

,'p_3.csv'

,'a_1.csv'

]#先按照乙個排序,再按照乙個排序

x1 = lambda x:

len(x)#以長度為key

x2 = lambda x: x.

split

('_')[

-1].

split

('.')[

0]x3 = lambda x: x.

split

('_')[

0]#以首字母為key

x4 = lambda x:

(x[0

], x[1]

)#先以x[0]

,後以x[

1]排序

stat lightgbm.py

out:

file: 『lightgbm.py』

size:

23911 blocks:

48io block:

4096 regular file

device:

811h/

2065d inode:

2406583579 links:

1access:

(0775

/-rwxrwxr-x) uid:

(1010

(1011

access:

2019-11

-2007:

21:28.046885416

-0500

modify:

2019-11

-2007:

21:28.046885416

-0500

change:

2019-11

-2007:

21:28.047885419

-0500

du -a
date

out: mon nov 2519:

16:47est

2019

SAS data步的操作技能點 3

1 日期計算 dataage infile e 11 6 tomdate.dat input 1randdate mmddyy10.12brthdate mmddyy10.23date12 mmddyy10.agedays randdate brthdate ageyrs randdate brth...

Python基礎之檔案操作

python的檔案操作還是比較簡單的基本分為三步 開啟,讀寫,關閉 檔案開啟 f open db r 開啟 f.read 讀 f.close 關閉 為了避免程式設計師忘記最後一步關閉,優雅的python使用了以下語句 with open db r as f,open db2 r as f2 opra...

python基礎之檔案操作

開啟檔案並輸出檔案內容模板 f open flie mode r encoding utf 8 data f.read 返回整個檔案資料 data f.readlines 以列 式,返回整個檔案資料 print data f.close flie檔案路徑 相對路徑或者絕對路徑 mode檔案開啟模式 ...