[英]Add column from one .csv to another .csv file using Python
我目前正在編寫一個腳本,在其中創建一個由其他csv文件列和我自己創建的列組成的csv文件('tableau_input.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'中的數據添加到'Datetime_filename'中,但是當我打開'tableau_input'文件時,'Date'列中沒有數據。 有人可以解決這個問題嗎?
有關信息,下面分別是“ game_data_filename”和“ datetime_filename”的csv文件的屏幕截圖(nb:datetime值采用datetime格式)
很難測試一下,因為我真的不知道輸入應該是什么樣,但是嘗試這樣的操作:
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, open(datetime_filename, 'rb') as readfile2:
scoreReader = csv.reader(readfile)
scores = [row for row in scoreReader]
scores = scores[1::]
daysReader = csv.reader(readfile2)
days = [day for day in daysReader]
if(len(scores) != len(days)):
print("File lengths do not match")
else:
for i in range(len(days)):
tableau_content = scores[i][1::]
tableau_date = days[i]
# 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_content += tableau_date
tableau_write.writerow(tableau_content)
這將兩個文件讀取部分合並為一個。
根據您的以下問題:
scoreReader = csv.reader(readfile)
scores = [row for row in scoreReader]
scores = scores[1::]
這使用列表理解來創建一個名為scores
的列表,每個元素都是scoreReader
中的行scoreReader
。 因為scorereader
是一個generator
,所以每當我們要求它連續一次時,它都會為我們吐出一個,直到沒有更多了。
第二行scores = scores[1::]
僅將列表的第一個元素切掉,因為您不需要標題。
有關更多信息,請嘗試以下方法:
祝好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.