1 2 3 4 file = r"D:\Stock\market_data\sh.600000.csv" df = pd.read_csv(file) df.tail()
date
code
open
high
low
close
preclose
volume
amount
adjustflag
turn
tradestatus
pctChg
peTTM
pbMRQ
isST
2102
2023-10-16
sh.600000
7.12
7.13
7.04
7.07
7.10
24907733
1.760976e+08
2
0.0849
1
-0.4225
4.701935
0.345631
0
2103
2023-10-17
sh.600000
7.09
7.10
7.05
7.09
7.07
19029143
1.346301e+08
2
0.0648
1
0.2829
4.715236
0.346609
0
2104
2023-10-18
sh.600000
7.07
7.11
7.05
7.05
7.09
21485721
1.520630e+08
2
0.0732
1
-0.5642
4.688634
0.344653
0
2105
2023-10-19
sh.600000
7.04
7.05
6.83
6.84
7.05
61679771
4.259569e+08
2
0.2101
1
-2.9787
4.548972
0.334387
0
2106
2023-10-20
sh.600000
6.81
6.93
6.80
6.87
6.84
31067950
2.132038e+08
2
0.1058
1
0.4386
4.568924
0.335853
0
使用Pandas追加数据行到源文件中
举例: 将最后三行再次追加到文件末尾
恢复源文件,删除刚才追加进去的数据行
date
code
open
high
low
close
preclose
volume
amount
adjustflag
turn
tradestatus
pctChg
peTTM
pbMRQ
isST
2104
2023-10-18
sh.600000
7.07
7.11
7.05
7.05
7.09
21485721
1.520630e+08
2
0.0732
1
-0.5642
4.688634
0.344653
0
2105
2023-10-19
sh.600000
7.04
7.05
6.83
6.84
7.05
61679771
4.259569e+08
2
0.2101
1
-2.9787
4.548972
0.334387
0
2106
2023-10-20
sh.600000
6.81
6.93
6.80
6.87
6.84
31067950
2.132038e+08
2
0.1058
1
0.4386
4.568924
0.335853
0
1 2 3 4 5 6 7 8 9 10 11 12 13 df1.to_csv(file, mode="a" , index=False , header=0 ) tmp = pd.read_csv(file) tmp.tail(10 ) tmp.drop_duplicates(subset='date' ,keep='first' ,inplace=True ) tmp.tail(10 ) tmp.to_csv(file,index=False , encoding='utf-8-sig' )
date
code
open
high
low
close
preclose
volume
amount
adjustflag
turn
tradestatus
pctChg
peTTM
pbMRQ
isST
2100
2023-10-12
sh.600000
7.09
7.17
7.06
7.14
7.02
36346964
2.592231e+08
2
0.1238
1
1.7094
4.748488
0.349053
0
2101
2023-10-13
sh.600000
7.11
7.15
7.08
7.10
7.14
19650410
1.397720e+08
2
0.0669
1
-0.5602
4.721886
0.347097
0
2102
2023-10-16
sh.600000
7.12
7.13
7.04
7.07
7.10
24907733
1.760976e+08
2
0.0849
1
-0.4225
4.701935
0.345631
0
2103
2023-10-17
sh.600000
7.09
7.10
7.05
7.09
7.07
19029143
1.346301e+08
2
0.0648
1
0.2829
4.715236
0.346609
0
2104
2023-10-18
sh.600000
7.07
7.11
7.05
7.05
7.09
21485721
1.520630e+08
2
0.0732
1
-0.5642
4.688634
0.344653
0
2105
2023-10-19
sh.600000
7.04
7.05
6.83
6.84
7.05
61679771
4.259569e+08
2
0.2101
1
-2.9787
4.548972
0.334387
0
2106
2023-10-20
sh.600000
6.81
6.93
6.80
6.87
6.84
31067950
2.132038e+08
2
0.1058
1
0.4386
4.568924
0.335853
0
2107
2023-10-18
sh.600000
7.07
7.11
7.05
7.05
7.09
21485721
1.520630e+08
2
0.0732
1
-0.5642
4.688634
0.344653
0
2108
2023-10-19
sh.600000
7.04
7.05
6.83
6.84
7.05
61679771
4.259569e+08
2
0.2101
1
-2.9787
4.548972
0.334387
0
2109
2023-10-20
sh.600000
6.81
6.93
6.80
6.87
6.84
31067950
2.132038e+08
2
0.1058
1
0.4386
4.568924
0.335853
0
date
code
open
high
low
close
preclose
volume
amount
adjustflag
turn
tradestatus
pctChg
peTTM
pbMRQ
isST
2097
2023-10-09
sh.600000
7.08
7.08
7.00
7.03
7.10
27763130
1.952144e+08
2
0.0946
1
-0.9859
4.675332
0.343675
0
2098
2023-10-10
sh.600000
7.13
7.14
7.01
7.01
7.03
24095561
1.698562e+08
2
0.0821
1
-0.2845
4.662031
0.342698
0
2099
2023-10-11
sh.600000
7.06
7.08
7.01
7.02
7.01
24057412
1.693714e+08
2
0.0820
1
0.1427
4.668682
0.343187
0
2100
2023-10-12
sh.600000
7.09
7.17
7.06
7.14
7.02
36346964
2.592231e+08
2
0.1238
1
1.7094
4.748488
0.349053
0
2101
2023-10-13
sh.600000
7.11
7.15
7.08
7.10
7.14
19650410
1.397720e+08
2
0.0669
1
-0.5602
4.721886
0.347097
0
2102
2023-10-16
sh.600000
7.12
7.13
7.04
7.07
7.10
24907733
1.760976e+08
2
0.0849
1
-0.4225
4.701935
0.345631
0
2103
2023-10-17
sh.600000
7.09
7.10
7.05
7.09
7.07
19029143
1.346301e+08
2
0.0648
1
0.2829
4.715236
0.346609
0
2104
2023-10-18
sh.600000
7.07
7.11
7.05
7.05
7.09
21485721
1.520630e+08
2
0.0732
1
-0.5642
4.688634
0.344653
0
2105
2023-10-19
sh.600000
7.04
7.05
6.83
6.84
7.05
61679771
4.259569e+08
2
0.2101
1
-2.9787
4.548972
0.334387
0
2106
2023-10-20
sh.600000
6.81
6.93
6.80
6.87
6.84
31067950
2.132038e+08
2
0.1058
1
0.4386
4.568924
0.335853
0
不使用Pandas,直接使用 readlines()
函数
获取文本文件的行列表
将列表的最后三个追加到原列表中
将新列表写入到文件中
1 2 3 4 5 6 with open (file,'r' ,encoding='utf-8' ) as f: lst = f.readlines() lst[-10 :]
['2023-10-09,sh.600000,7.08,7.08,7.0,7.03,7.1,27763130,195214392.39,2,0.0946,1,-0.9859,4.675332,0.343675,0\n',
'2023-10-10,sh.600000,7.13,7.14,7.01,7.01,7.03,24095561,169856240.67,2,0.0821,1,-0.2845,4.662031,0.342698,0\n',
'2023-10-11,sh.600000,7.06,7.08,7.01,7.02,7.01,24057412,169371363.46,2,0.082,1,0.1427,4.668682,0.343187,0\n',
'2023-10-12,sh.600000,7.09,7.17,7.06,7.14,7.02,36346964,259223143.18,2,0.1238,1,1.7094,4.748488,0.349053,0\n',
'2023-10-13,sh.600000,7.11,7.15,7.08,7.1,7.14,19650410,139771981.93,2,0.0669,1,-0.5602,4.721886,0.347097,0\n',
'2023-10-16,sh.600000,7.12,7.13,7.04,7.07,7.1,24907733,176097574.11,2,0.0849,1,-0.4225,4.701935,0.345631,0\n',
'2023-10-17,sh.600000,7.09,7.1,7.05,7.09,7.07,19029143,134630097.48,2,0.0648,1,0.2829,4.715236,0.346609,0\n',
'2023-10-18,sh.600000,7.07,7.11,7.05,7.05,7.09,21485721,152063023.64,2,0.0732,1,-0.5642,4.688634,0.344653,0\n',
'2023-10-19,sh.600000,7.04,7.05,6.83,6.84,7.05,61679771,425956887.36,2,0.2101,1,-2.9787,4.548972,0.334387,0\n',
'2023-10-20,sh.600000,6.81,6.93,6.8,6.87,6.84,31067950,213203780.58,2,0.1058,1,0.4386,4.568924,0.335853,0\n']
['2023-10-18,sh.600000,7.07,7.11,7.05,7.05,7.09,21485721,152063023.64,2,0.0732,1,-0.5642,4.688634,0.344653,0\n',
'2023-10-19,sh.600000,7.04,7.05,6.83,6.84,7.05,61679771,425956887.36,2,0.2101,1,-2.9787,4.548972,0.334387,0\n',
'2023-10-20,sh.600000,6.81,6.93,6.8,6.87,6.84,31067950,213203780.58,2,0.1058,1,0.4386,4.568924,0.335853,0\n']
1 2 3 new = lst + add new[-10 :]
['2023-10-12,sh.600000,7.09,7.17,7.06,7.14,7.02,36346964,259223143.18,2,0.1238,1,1.7094,4.748488,0.349053,0\n',
'2023-10-13,sh.600000,7.11,7.15,7.08,7.1,7.14,19650410,139771981.93,2,0.0669,1,-0.5602,4.721886,0.347097,0\n',
'2023-10-16,sh.600000,7.12,7.13,7.04,7.07,7.1,24907733,176097574.11,2,0.0849,1,-0.4225,4.701935,0.345631,0\n',
'2023-10-17,sh.600000,7.09,7.1,7.05,7.09,7.07,19029143,134630097.48,2,0.0648,1,0.2829,4.715236,0.346609,0\n',
'2023-10-18,sh.600000,7.07,7.11,7.05,7.05,7.09,21485721,152063023.64,2,0.0732,1,-0.5642,4.688634,0.344653,0\n',
'2023-10-19,sh.600000,7.04,7.05,6.83,6.84,7.05,61679771,425956887.36,2,0.2101,1,-2.9787,4.548972,0.334387,0\n',
'2023-10-20,sh.600000,6.81,6.93,6.8,6.87,6.84,31067950,213203780.58,2,0.1058,1,0.4386,4.568924,0.335853,0\n',
'2023-10-18,sh.600000,7.07,7.11,7.05,7.05,7.09,21485721,152063023.64,2,0.0732,1,-0.5642,4.688634,0.344653,0\n',
'2023-10-19,sh.600000,7.04,7.05,6.83,6.84,7.05,61679771,425956887.36,2,0.2101,1,-2.9787,4.548972,0.334387,0\n',
'2023-10-20,sh.600000,6.81,6.93,6.8,6.87,6.84,31067950,213203780.58,2,0.1058,1,0.4386,4.568924,0.335853,0\n']
1 2 3 4 5 6 7 8 with open (file,'w' ,encoding='utf-8' ) as f: f.writelines(new) f.close() pd.read_csv(file).tail(10 )
date
code
open
high
low
close
preclose
volume
amount
adjustflag
turn
tradestatus
pctChg
peTTM
pbMRQ
isST
2100
2023-10-12
sh.600000
7.09
7.17
7.06
7.14
7.02
36346964
2.592231e+08
2
0.1238
1
1.7094
4.748488
0.349053
0
2101
2023-10-13
sh.600000
7.11
7.15
7.08
7.10
7.14
19650410
1.397720e+08
2
0.0669
1
-0.5602
4.721886
0.347097
0
2102
2023-10-16
sh.600000
7.12
7.13
7.04
7.07
7.10
24907733
1.760976e+08
2
0.0849
1
-0.4225
4.701935
0.345631
0
2103
2023-10-17
sh.600000
7.09
7.10
7.05
7.09
7.07
19029143
1.346301e+08
2
0.0648
1
0.2829
4.715236
0.346609
0
2104
2023-10-18
sh.600000
7.07
7.11
7.05
7.05
7.09
21485721
1.520630e+08
2
0.0732
1
-0.5642
4.688634
0.344653
0
2105
2023-10-19
sh.600000
7.04
7.05
6.83
6.84
7.05
61679771
4.259569e+08
2
0.2101
1
-2.9787
4.548972
0.334387
0
2106
2023-10-20
sh.600000
6.81
6.93
6.80
6.87
6.84
31067950
2.132038e+08
2
0.1058
1
0.4386
4.568924
0.335853
0
2107
2023-10-18
sh.600000
7.07
7.11
7.05
7.05
7.09
21485721
1.520630e+08
2
0.0732
1
-0.5642
4.688634
0.344653
0
2108
2023-10-19
sh.600000
7.04
7.05
6.83
6.84
7.05
61679771
4.259569e+08
2
0.2101
1
-2.9787
4.548972
0.334387
0
2109
2023-10-20
sh.600000
6.81
6.93
6.80
6.87
6.84
31067950
2.132038e+08
2
0.1058
1
0.4386
4.568924
0.335853
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 with open (file,'r' ,encoding='utf-8' ) as f: lst = f.readlines() f.close() new = lst[:-3 ] new[-10 :] with open (file,'w' ,encoding='utf-8' ) as f: f.writelines(new) f.close() pd.read_csv(file).tail(10 )
['2023-10-09,sh.600000,7.08,7.08,7.0,7.03,7.1,27763130,195214392.39,2,0.0946,1,-0.9859,4.675332,0.343675,0\n',
'2023-10-10,sh.600000,7.13,7.14,7.01,7.01,7.03,24095561,169856240.67,2,0.0821,1,-0.2845,4.662031,0.342698,0\n',
'2023-10-11,sh.600000,7.06,7.08,7.01,7.02,7.01,24057412,169371363.46,2,0.082,1,0.1427,4.668682,0.343187,0\n',
'2023-10-12,sh.600000,7.09,7.17,7.06,7.14,7.02,36346964,259223143.18,2,0.1238,1,1.7094,4.748488,0.349053,0\n',
'2023-10-13,sh.600000,7.11,7.15,7.08,7.1,7.14,19650410,139771981.93,2,0.0669,1,-0.5602,4.721886,0.347097,0\n',
'2023-10-16,sh.600000,7.12,7.13,7.04,7.07,7.1,24907733,176097574.11,2,0.0849,1,-0.4225,4.701935,0.345631,0\n',
'2023-10-17,sh.600000,7.09,7.1,7.05,7.09,7.07,19029143,134630097.48,2,0.0648,1,0.2829,4.715236,0.346609,0\n',
'2023-10-18,sh.600000,7.07,7.11,7.05,7.05,7.09,21485721,152063023.64,2,0.0732,1,-0.5642,4.688634,0.344653,0\n',
'2023-10-19,sh.600000,7.04,7.05,6.83,6.84,7.05,61679771,425956887.36,2,0.2101,1,-2.9787,4.548972,0.334387,0\n',
'2023-10-20,sh.600000,6.81,6.93,6.8,6.87,6.84,31067950,213203780.58,2,0.1058,1,0.4386,4.568924,0.335853,0\n']
date
code
open
high
low
close
preclose
volume
amount
adjustflag
turn
tradestatus
pctChg
peTTM
pbMRQ
isST
2097
2023-10-09
sh.600000
7.08
7.08
7.00
7.03
7.10
27763130
1.952144e+08
2
0.0946
1
-0.9859
4.675332
0.343675
0
2098
2023-10-10
sh.600000
7.13
7.14
7.01
7.01
7.03
24095561
1.698562e+08
2
0.0821
1
-0.2845
4.662031
0.342698
0
2099
2023-10-11
sh.600000
7.06
7.08
7.01
7.02
7.01
24057412
1.693714e+08
2
0.0820
1
0.1427
4.668682
0.343187
0
2100
2023-10-12
sh.600000
7.09
7.17
7.06
7.14
7.02
36346964
2.592231e+08
2
0.1238
1
1.7094
4.748488
0.349053
0
2101
2023-10-13
sh.600000
7.11
7.15
7.08
7.10
7.14
19650410
1.397720e+08
2
0.0669
1
-0.5602
4.721886
0.347097
0
2102
2023-10-16
sh.600000
7.12
7.13
7.04
7.07
7.10
24907733
1.760976e+08
2
0.0849
1
-0.4225
4.701935
0.345631
0
2103
2023-10-17
sh.600000
7.09
7.10
7.05
7.09
7.07
19029143
1.346301e+08
2
0.0648
1
0.2829
4.715236
0.346609
0
2104
2023-10-18
sh.600000
7.07
7.11
7.05
7.05
7.09
21485721
1.520630e+08
2
0.0732
1
-0.5642
4.688634
0.344653
0
2105
2023-10-19
sh.600000
7.04
7.05
6.83
6.84
7.05
61679771
4.259569e+08
2
0.2101
1
-2.9787
4.548972
0.334387
0
2106
2023-10-20
sh.600000
6.81
6.93
6.80
6.87
6.84
31067950
2.132038e+08
2
0.1058
1
0.4386
4.568924
0.335853
0