實驗數據-在/tmp目錄下有文件stud_info.csvpython
統計文件行數:cat stud_info.csv|wc -w mysql
查看文件前5行內容:head -5 stud_info.csvsql
導出數據shell
mysql> select * from stud_info into outfile '/tmp/stud_info_wu1.txt' fields terminated by "," enclosed by "";數據庫
註釋:terminated by ","表示導出來的文件列與列之間用逗號分隔;enclosed by ""表示導出來的每一列不加任何東西bash
In [1]: import numpy as np In [2]: import pandas as pd In [3]: from pandas import DataFrame In [4]: inputfile1='/tmp/stud_info.csv' In [5]: data = pd.read_csv(inputfile1) In [6]: df = DataFrame(data)
In [8]: df.columns
In [9]: df.head(2)
In [10]: df.tail(2)
spa
wc命令命令行
格式:wc -【參數】filecode
wc -l file # 顯示文件行數blog
wc -w file #打印單詞數
練習:
編寫一個shell腳本把stud_info.csv導入表(表各自定義);把stud_info.csv導入pandas;驗證導入數據的正確性。
1 #!/bin/bash 2 #文件名稱:import_mysql 3 #文件功能:將stud_info.csv導入stud_info表中 4 #建立做者:鄔家棟 5 #建立時間:2016-07-29 6 7 rows_file=`cat /tmp/stud_info.csv|wc -l` #注意這裏是反引號 8 echo "stud_info.csv一共有${rows_file}行" 9 10 #要先鏈接數據庫 11 mysql -u feigu_mysql -p testdb << EOF 12 drop table if exists wu_stud_info; 13 create table wu_stud_info( 14 stud_code varchar(20) not null, 15 stud_name varchar(100) not null, 16 stud_gend varchar(10) not null default "M", 17 birthday date null, 18 log_date date null, 19 orig_addr varchar(60) null, 20 lev_date date null, 21 colledge_code varchar(10) null, 22 colledge_name varchar(300) null, 23 state varchar(20) null, 24 primary key(stud_code) 25 ); 26 27 load data infile '/tmp/stud_info.csv' into table wu_stud_info fields terminated by ',' ignore 1 lines; 28 29 EOF 30 31 if [ $? -eq 0 ] ; then 32 echo "操做成功" 33 else 34 echo "操做失敗" 35 36 fi
註釋:在寫默認值的時候,默認值須要用引號引發來。
注意:if語句必定不要忘記寫fi結尾。if後面中括號需留空格
1 #!/bin/bash 2 #文件名稱:pandas_import.sh 3 #文件功能:將stud_info.csv 導入到padans中 4 #建立時間:2016-07-29 5 #建立者:鄔家棟 6 7 ipython << EOF 8 9 import numpy as np 10 import pandas as pd 11 from pandas import DataFrame 12 inputfile1 = '/tmp/stud_info.csv' 13 data= pd.read_csv(inputfile1) 14 df=DataFrame(data) 15 16 EOF 17 18 if [ $? -eq 0 ] ;then 19 echo "操做成功" 20 else 21 echo "操做失敗" 22 fi