caiying069 发表于 2020-5-20 13:37:52

关于读取scv文件会出现第一个空格的问题

关于读取scv文件会出现第一个空格的问题
我在照着视频写带吗的时候出现这样一个情况
# 画图
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.pylab as lab
import mplfinance as mpl
import os

# print(mpf.available_styles())
# 创建画图区域
# fig, axs = plt.subplots(2, 1, sharex=True)
# ax1, ax2 = axs
# 画K线图
code = input("请输入一个股票代码:") # 导入代码code
# 读取文件csv
if os.path.exists(f"C:/Users/Administrator/PycharmProjects/学习库/股票爬虫/{code}.csv"):
    # 设置data,并且设置时间格式
    data = pd.read_csv(f"C:/Users/Administrator/PycharmProjects/学习库/股票爬虫/{code}.csv").iloc[:30, :]
    # k线图数据输入是按"开盘价", "收盘价", "最高价", "最低价"排序
    # 数据列名必须为dete
    data = data[["日期", "开盘价", "收盘价", "最高价", "最低价", "成交量"]]
    data = data.rename(
      columns={"日期": "Date","开盘价": "Open",
               "收盘价": "Close",
               "最高价": "High",
               "最低价": "Lower",
               "成交量": "Volume"
               })
    # 设置时间索引(必须)
    data = data.set_index(["Date"])
    print(data)
    # 画蜡烛图
    mpl.plot(data)

else:
    print("请重新输入")

# 画柱状图
plt.show()

csv文件内容如下
日期,股票代码,名称,收盘价,最高价,最低价,开盘价,前收盘,涨跌额,涨跌幅,换手率,成交量,成交金额,总市值,流通市值

2020-05-18,600000,浦发银行,10.32,10.43,10.25,10.28,10.28,0.04,0.3891,0.0558,15675941,162140715.0,3.02913469697e+11,2.90030843438e+11

2020-05-15,600000,浦发银行,10.28,10.38,10.26,10.36,10.3,-0.02,-0.1942,0.0489,13753770,141846602.0,3.01739386481e+11,2.88906692882e+11

2020-05-14,600000,浦发银行,10.3,10.36,10.27,10.32,10.38,-0.08,-0.7707,0.0565,15892376,163558192.0,3.02326428089e+11,2.8946876816e+11

2020-05-13,600000,浦发银行,10.38,10.39,10.28,10.31,10.34,0.04,0.3868,0.0554,15574310,160783648.0,3.04674594521e+11,2.91717069272e+11

2020-05-12,600000,浦发银行,10.34,10.48,10.34,10.44,10.43,-0.09,-0.8629,0.0691,19405945,201474588.0,3.03500511305e+11,2.90592918716e+11

2020-05-11,600000,浦发银行,10.43,10.54,10.42,10.44,10.44,-0.01,-0.0958,0.0764,21472495,224819916.0,3.06142198541e+11,2.93122257467e+11

2020-05-08,600000,浦发银行,10.44,10.49,10.4,10.44,10.39,0.05,0.4812,0.0745,20927670,218698514.0,3.06435719345e+11,2.93403295106e+11

2020-05-07,600000,浦发银行,10.39,10.46,10.37,10.45,10.46,-0.07,-0.6692,0.0933,26211656,272516386.0,3.04968115325e+11,2.91998106911e+11

2020-05-06,600000,浦发银行,10.46,10.49,10.36,10.44,10.63,-0.17,-1.5992,0.1472,41378362,431336740.0,3.07022760953e+11,2.93965370384e+11

2020-04-30,600000,浦发银行,10.63,10.79,10.56,10.58,10.61,0.02,0.1885,0.11,30906971,330157488.0,3.1201261462e+11,2.98743010246e+11

2020-04-29,600000,浦发银行,10.61,10.61,10.33,10.35,10.33,0.28,2.7106,0.1247,35056836,367739389.0,3.11425573012e+11,2.98180934968e+11

2020-04-28,600000,浦发银行,10.33,10.38,10.22,10.36,10.34,-0.01,-0.0967,0.104,29222578,301584635.0,3.03206990501e+11,2.90311881077e+11

2020-04-27,600000,浦发银行,10.34,10.42,10.15,10.15,10.06,0.28,2.7833,0.1791,50342806,520243997.0,3.03500511305e+11,2.90592918716e+11

2020-04-24,600000,浦发银行,10.06,10.09,10.01,10.06,10.06,0.0,0.0,0.0604,16973113,170599170.0,2.95281928794e+11,2.82723864824e+11

2020-04-23,600000,浦发银行,10.06,10.1,10.01,10.1,10.07,-0.01,-0.0993,0.0601,16878643,169579075.0,2.95281928794e+11,2.82723864824e+11

2020-04-22,600000,浦发银行,10.07,10.07,9.98,10.02,10.03,0.04,0.3988,0.0562,15802441,158372535.0,2.95575449598e+11,2.83004902463e+11

2020-04-21,600000,浦发银行,10.03,10.12,10.01,10.07,10.11,-0.08,-0.7913,0.0625,17557525,176508085.0,2.94401366382e+11,2.81880751907e+11

当我读取第一个文件之后出现,date列下沉一个单元格,其他的列保存完整,我不知道这是不是影响我代码运行的原因,请大神帮我解决这个问题,谢谢啦
>>>
================ RESTART: C:/Users/Administrator/Desktop/股票爬虫.py ===============
请输入一个股票代码:600000
            OpenClose   HighLower    Volume
Date                                          
2020-05-1810.2810.3210.4310.2515675941
2020-05-1510.3610.2810.3810.2613753770
2020-05-1410.3210.3010.3610.2715892376
2020-05-1310.3110.3810.3910.2815574310
2020-05-1210.4410.3410.4810.3419405945
2020-05-1110.4410.4310.5410.4221472495
2020-05-0810.4410.4410.4910.4020927670
2020-05-0710.4510.3910.4610.3726211656
2020-05-0610.4410.4610.4910.3641378362
2020-04-3010.5810.6310.7910.5630906971
2020-04-2910.3510.6110.6110.3335056836
2020-04-2810.3610.3310.3810.2229222578
2020-04-2710.1510.3410.4210.1550342806
2020-04-2410.0610.0610.0910.0116973113
2020-04-2310.1010.0610.1010.0116878643
2020-04-2210.0210.0710.07   9.9815802441
2020-04-2110.0710.0310.1210.0117557525
2020-04-2010.1510.1110.1910.0923503715
2020-04-1710.1810.2010.2310.1522744411
2020-04-1610.1010.1610.1810.1014403197
2020-04-1510.2010.1810.2710.1514945608
2020-04-1410.1710.2510.2510.1217792926
2020-04-1310.1510.1110.2210.1011966986
2020-04-1010.2210.2110.2410.1516625528
2020-04-0910.2610.2010.2910.1813569586
2020-04-0810.2510.2410.2910.1817952255
2020-04-0710.3010.2810.3310.2323825611
2020-04-0310.1310.1510.2110.1214842382
2020-04-0210.0910.2010.2010.0717042135
2020-04-0110.1110.0910.2610.0922479192
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/股票爬虫.py", line 32, in <module>
    mpl.plot(data)
File "C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages\mplfinance\plotting.py", line 46, in decorator
    return func(*args, **kwargs)
File "C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages\mplfinance\plotting.py", line 205, in plot
    dates,opens,highs,lows,closes,volumes = _check_and_prepare_data(data, config)
File "C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages\mplfinance\_arg_validators.py", line 27, in _check_and_prepare_data
    raise TypeError('Expect data.index as DatetimeIndex')
TypeError: Expect data.index as DatetimeIndex

xiaosi4081 发表于 2020-5-20 13:47:33

csv吧{:10_245:}

caiying069 发表于 2020-5-20 14:22:43

xiaosi4081 发表于 2020-5-20 13:47
csv吧

手快打错了 ,好几次了,怎么就管不住这个手呢

caiying069 发表于 2020-5-20 14:23:42

xiaosi4081 发表于 2020-5-20 13:47
csv吧

你知道这个问题怎么解决吗

Twilight6 发表于 2020-5-20 16:56:47

caiying069 发表于 2020-5-20 14:23
你知道这个问题怎么解决吗

没办法 这种数据处理类型的在论坛一般都解决率比较低
页: [1]
查看完整版本: 关于读取scv文件会出现第一个空格的问题