0
私のpythonコードは、.csvファイルからデータを取り込み、.odsファイルに変換して保存します。問題は、保存したファイル内の他のシートを削除する新しいファイルに保存するときです。他のシートを削除せずに新しいファイルを更新するにはどうすればよいですか?ファイル内の他のシートを破棄することなく、odsファイル内にシートを作成/投入するにはどうすればよいですか?
import csv, shutil, os, glob
from pyexcel_ods3 import save_data
from collections import OrderedDict
# finds the latest file from my downloads folder
newest = max(glob.iglob('*.csv'), key=os.path.getctime)
my_file = open(newest)
file_reader = csv.reader(my_file)
file_data = list(file_reader)
# identifies the data structure
file_header = ['Details', 'Posting Date', 'Description', 'Amount', 'Type', 'Balance', 'Check or Slip #']
# compares the data structure in the csv file to the one used for this script
if file_data[0] == file_header:
del file_data[0] # removes the header form the csv file
date = file_data[0][1]
f_date = date.split('/')
f_date_formatted = '{}-{}-{}'.format(f_date[2], f_date[0], f_date[1])
for i in file_data: # organizes the data
i[4] = ''
transaction = float(i[3])
if transaction > 0:
i[3] = ''
i[4] = transaction
my_file.close()
# creates a copy of the unpopulated file. This file has 3 existing sheets. One sheet is called 'DATA'
shutil.copy('Checkbook Monthly balance.ods', 'Checkbook Monthly balance thru {}.ods'.format(f_date_formatted))
# populates the sheet called 'DATA'
data = OrderedDict()
data.update({'DATA': file_data})
save_data('Checkbook Monthly balance thru {}.ods'.format(f_date_formatted), data)
else:
print('It appears the bank changed the table format. Check the .csv file for a difference in the data structure.')
my_file.close()
ご回答いただきありがとうございます。私はPythonを初めて熟知しており、これをどのようにフォーマットするのかと苦労しています。私はあなたが何を言っているのか理解しています。私はフォーマットの助けが必要です。 – draugr
get_data( 'Checkbook Monthly balance.ods')は辞書も返します:{'DATA':file_data} – chfw
私が望むことをやり遂げて他のシートを保持することはできましたが、他のシートからすべてのフォーマットを削除します。書式を維持する方法はありますか? – draugr