我希望我的代码能够完成以下任务:
我尝试了以下代码:
import glob
import numpy as np
import os
import pandas as pd
from os import listdir
# DEFINE PATH, STATE SUFFIX OF FILE, AND GET NUMBER OF FILES ENDING WITH SUFFIX
path = r'ledger'
suff = r'.csv'
file = listdir(path)
file = [item for item in file if item.endswith(suff)]
# CHECK IF LEDGER IS AVAILABLE, AND IF NOT, CREATE ONE
if len(file) == 0:
save = np.zeros([1, 7])
save = pd.DataFrame(save, columns = ['Year', 'Month', 'Day', 'Ticker', 'Type', 'Quantity', 'Value'])
print(save.shape)
print(save)
save.to_csv(os.path.join(path, r'ledger.csv'))
else:
load = pd.read_csv(os.path.join(path, r'ledger.csv'))
load = pd.DataFrame(load)
print(load.shape)
print(load)
如果文件不存在,则打印功能会产生以下内容:
(1, 7)
Year Month Day Ticker Type Quantity Value
0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
但是,如果文件存在,则读取文件并打印以下函数:
(1, 8)
Unnamed: 0 Year Month Day Ticker Type Quantity Value
0 0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
我不确定如何保持DataFrame的大小不变。在加载为DataFrame时,似乎会添加原始文件的索引。任何建议将不胜感激。
答案 0 :(得分:3)
我认为问题在于,当您写入文件时,您也会编写索引。
//get an external text file to display
$filename = "test.txt";
$textfile = file_get_contents($filename);
$section->addText($textfile);
这将阻止将索引写入文件。当您阅读此文件时,它应该有七列