私は現在、自分で作成したcsvファイルの列と列の両方を構成するcsvファイル( 'tableau_input.csv')を作成しています。私は次のコードを試しました:Pythonを使用して.csvファイルを別の.csvファイルに追加します
def make_tableau_file(mp, current_season = 2016):
# Produces a csv file containing predicted and actual game results for the current season
# Tableau uses the contents of the file to produce visualization
game_data_filename = 'game_data' + str(current_season) + '.csv'
datetime_filename = 'datetime' + str(current_season) + '.csv'
with open('tableau_input.csv', 'wb') as writefile:
tableau_write = csv.writer(writefile)
tableau_write.writerow(['Visitor_Team', 'V_Team_PTS', 'Home_Team', 'H_Team_PTS', 'True_Result', 'Predicted_Results', 'Confidence', 'Date'])
with open(game_data_filename, 'rb') as readfile:
scores = csv.reader(readfile)
scores.next()
for score in scores:
tableau_content = score[1::]
# Append True_Result
if int(tableau_content[3]) > int(tableau_content[1]):
tableau_content.append(1)
else:
tableau_content.append(0)
# Append 'Predicted_Result' and 'Confidence'
prediction_results = mp.make_predictions(tableau_content[0], tableau_content[2])
tableau_content += list(prediction_results)
tableau_write.writerow(tableau_content)
with open(datetime_filename, 'rb') as readfile2:
days = csv.reader(readfile2)
days.next()
for day in days:
tableau_write.writerow(day)
'tableau_input.csv'は私が作成しているファイルです。 'Visitor_Team'、 'V_Team_PTS'、 'Home_Team'、 'H_Team_PTS'の列は 'game_data_filename'(tableau_content = score [1 ::]など)からのものです。 'True_Result'、 'Predicted_Results'、 'Confidence'の列は、最初のforループで作成された列です。 これまでの作業はすべて機能しますが、最後に、上記と同じ構造を使用して 'datetime_filename'の 'Date'列データに追加しようとしましたが、 'tableau_input'ファイルを開くと、 'Date' 。誰かがこの問題を解決できますか?以下の情報については
は、 'game_data_filename' と 'datetime_filename' のそれぞれのcsvファイルのスクリーンショット(NB:datetime値は、日時書式である)ある
デバッガを使って何を試しましたか?ファイルは 'readfile2'の内容で開きますか?また、これはおそらく、すべての入力行を最初に書き、次にすべての日付行を書き出します。両方のファイルを開き、両方を一緒に追加してから書き込みたいのですか? –
はい私はreadfile2(datetime値)にコンテンツを持っていますし、基本的には新しいCSVファイル 'tableau_input'に両方のファイルの内容を追加するという考え方です。私はそれを認識しませんでした。これを達成する方法がありますか? – DiamondDogs95
ファイルの末尾に日付行がありますか? –