Python Forum
Trouble with reading csv file and putting it into a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with reading csv file and putting it into a file
#1
I am reading a csv file. I am then putting it into a dictionary and then trying to put the Dictionary into a list. Problem is, in the file I am reading there should be 8 different dictionaries put in the list. But the only one that shows up is the last one, number 8. Anyway here is the code.
# DEFINE LOAD RACE FILE FUNCTION
def load_racefiles() :
	count = 0
	master_race_file = []
	rows = 100
	columns = 100
	horse_array = [0 for i in range(rows)] 
	file_name = StringVar()
	f_name = StringVar()
	#file_dir = (r"C:\2020")
	fd =    open("dirpath", "r") 
	file_dir = (fd.read())
	fd.close()
	os.chdir(file_dir)
	for f in os.listdir(file_dir): 
		file_name, file_ext = os.path.splitext(f)
		f_name = file_name + file_ext
		file_name = file_name.strip()[3:]
		line_counter = 0
		if file_name == Race_date and file_ext == ".jcp":
			with open(f_name, "r") as csv_file:
				csv_reader = csv.reader(csv_file)
				
				for line in csv_reader:
					
					
					

					track_name = line[0]
					race_num =  line[2]
					surface =  line[6]
					race_type =  line[8]
					purse = line[11]
					trn_starts_curmeet = line[28]
					trn_wins_curmeet = line[29]
					jock_sts_curmeet =  line[34]
					jock_wins_curmeet =  line[35]
					trn_starts_curyear = line[1146]
					trn_wins_curyear = line[1147]
					jock_sts_curyear =  line[1156]
					jock_wins_curyear =  line[1157]
					trn_starts_prevyear = line[1151]
					trn_wins_prevyear = line[1152]
					jock_sts_prevyear =  line[1161]
					jock_wins_prevyear =  line[1162]
					morn_line = line[43]
					horse_name  = line[44]
					year_of_birth = line[45]
					hstarts_todays_dist = line[64]
					hwins_todays_dist = line[65]
					hstarts_todays_track = line[69]
					hwins_todays_track = line[70]
					hstarts_turf = line[74]
					hwins_turf = line[75]
					hstarts_mud = line[79]
					hwins_mud = line[80]
					hstarts_cur_yr = line[84]
					hwins_cur_yr = line[85]
					hstarts_prev_yr = line[90]
					hwins_prev_yr = line[91]
					hstarts_life = line[96]
					hwins_life = line[97]
					days_since = line[223]
					power_rating = line[250]
					dist_yards1 = line[315]
					dist_yards2 = line[316]
					dist_yards3 = line[317]
					dist_yards4 = line[318]
					dist_yards5 = line[319]
					dist_yards6 = line[320]
					dist_yards7 = line[321]
					dist_yards8 = line[322]
					dist_yards9 = line[323]
					dist_yards10 = line[324]
					surface1 = line[325]
					surface2 = line[326]
					surface3 = line[327]
					surface4 = line[328]
					surface5 = line[329]
					surface6 = line[330]
					surface7 = line[331]
					surface8 = line[332]
					surface9 = line[333]
					surface10 = line[334]
					entrants1 =  line[345]
					entrants2 =  line[346]
					entrants3 =  line[347]
					entrants4 =  line[348]
					entrants5 =  line[349]
					entrants6 =  line[350]
					entrants7 =  line[351]
					entrants8 =  line[352]
					entrants9 =  line[353]
					entrants10 =  line[354]
					first_call1 = line[575]
					first_call2 = line[576]
					first_call3 = line[577]
					first_call4 = line[578]
					first_call5 = line[579]
					first_call6 = line[580]
					first_call7 = line[581]
					first_call8 = line[582]
					first_call9 = line[583]
					first_call10 = line[584]
					second_call1 = line[585]
					second_call2 = line[586]
					second_call3 = line[587]
					second_call4 = line[588]
					second_call5 = line[589]
					second_call6 = line[590]
					second_call7 = line[591]
					second_call8 = line[592]
					second_call9 = line[593]
					second_call0 = line[594]
					finish_Position = line[615]
					last_peed = line[845]
					speed_2back = line[846]
					speed_3back = line[847]
					bestSpeed_Life  = line[1327]
					bestSpeed_Fasttrack = line[1177]
					bestSpeed_turf = line[1178]
					bestSpeed_offtrack = line[1179]
					bestSpeed_dist = line[1180]
					race_conditions = line[15]
					todays_race_classification = line[10]
					
					

					
					h = {'track' : track_name,
					'race_number' : race_num, 
					'todays_surface' : surface,
					'race_type' : race_type, 
					'todays_purse' : purse,
					'trn_starts_curmeet' : trn_starts_curmeet,
					'trn_wins_curmeet' : trn_wins_curmeet,
					'jock_sts_curmeet' : jock_sts_curmeet,
					'jock_wins_curmeet' : jock_wins_curmeet,
					'trn_starts_prevyear' : trn_starts_prevyear,
					'trn_wins_prevyear' : trn_wins_prevyear,
					'jock_sts_prevyear' : jock_sts_prevyear,
					'jock_wins_prevyear' : jock_wins_prevyear,
					'morn_line' : morn_line,
					'horse_name' : horse_name,
					'year_of_birth' : year_of_birth,
					'hstarts_todays_dist' : hstarts_todays_dist,
					'hwins_todays_dist ' : hwins_todays_dist,
					'hstarts_todays_track' : hstarts_todays_track,
					'hwins_todays_track' :hwins_todays_track,
					'hstarts_turf' : hstarts_turf,
					'hwins_turf' : hwins_turf,
					'hstarts_mud' : hstarts_mud,
					'hwins_mud' : hwins_mud,
					'hstarts_cur_yr' : hstarts_cur_yr,
					'hwins_cur_yr' : hwins_cur_yr,
					'hstarts_prev_yr' : hstarts_cur_yr,
					'hwins_prev_yr' : hwins_cur_yr,
					'hstarts_life' : hstarts_life,
					'hwins_life' : hwins_life, 
					'days_since' : days_since,
					'power_rating' : power_rating,
					'dist_yards1' : dist_yards1,
					'dist_yards2' : dist_yards2,
					'dist_yards3' : dist_yards3,
					'dist_yards4' : dist_yards4,
					'dist_yards5' : dist_yards5,
					'dist_yards6' : dist_yards6,
					'dist_yards7' : dist_yards7,
					'dist_yards8' : dist_yards8,
					'dist_yards9' : dist_yards9,
					'dist_yards10' : dist_yards10,
					'surface1' : surface1,
					'surface2' : surface2,
					'surface3' : surface3,
					'surface4' : surface4,
					'surface5' : surface5,
					'surface6' : surface6,
					'surface7' : surface7,
					'surface8' : surface8, 
					'surface9' : surface8,
					'surface10' : surface10,
					'entrants1' : entrants1,
					'entrants2' : entrants2,
					'entrants3' : entrants3,
					'entrants4' : entrants4,
					'entrants5' : entrants5,
					'entrants6' : entrants6,
					'entrants7' : entrants7,
					'entrants8' : entrants8,
					'entrants9' : entrants9,
					'entrants10' : entrants10,
					'first_call1' : first_call1,
					'first_call2' : first_call2,
					'first_call3' : first_call3,
					'first_call4' : first_call4,
					'first_call5' : first_call5,
					'first_call6' : first_call6,
					'first_call7' : first_call7,
					'first_call8' : first_call8,
					'first_call9' : first_call9,
					'first_call10' : first_call10,
					'second_call1' : second_call1,
					'second_call2' : second_call2,
					'second_call3' : second_call3,
					'second_call4' : second_call4,
					'second_call5' : second_call5,
					'second_call6' : second_call6,
					'second_call7' : second_call7,
					'second_call8' : second_call8,
					'second_call9' : second_call9,
					'second_call0' : second_call0,
					'finish_Position' : finish_Position,
					'last_peed' : last_peed,
					'speed_2back' : speed_2back,
					'speed_3back' : speed_3back,
					'trn_starts_curyear' : trn_starts_curyear,
					'trn_wins_curyear' : trn_wins_curyear,
					'jock_sts_curyear' : jock_sts_curyear,
					'jock_wins_curyear' : jock_wins_curyear,
					'bestSpeed_Life' : bestSpeed_Life,
					'bestSpeed_Fasttrack' : bestSpeed_Fasttrack,
					'bestSpeed_turf' : bestSpeed_turf,
					'bestSpeed_offtrack' : bestSpeed_offtrack,
					'bestSpeed_dist' : bestSpeed_dist,
					'race_conditions' : race_conditions,
					'todays_race_classification' : todays_race_classification

					

 				
 						}

				master_race_file.append(h ) 
				line_counter +=1
				
	
	 					
				


	print(master_race_file)
	print(line_counter)
				         		
						
	my_labeldone= Label( text ="Downloading Complete", fg= "black", font = ("sans_serif" , 16)).place(x=500, y=500)					

  
I don't understand why just the last one prints out when I print the whole list.
Reply


Messages In This Thread
Trouble with reading csv file and putting it into a file - by Milfredo - Sep-03-2020, 08:19 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  file open "file not found error" shanoger 8 1,146 Dec-14-2023, 08:03 AM
Last Post: shanoger
Sad problems with reading csv file. MassiJames 3 645 Nov-16-2023, 03:41 PM
Last Post: snippsat
  trouble reading string/module from excel as a list popular_dog 0 429 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  Need to replace a string with a file (HTML file) tester_V 1 775 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Reading a file name fron a folder on my desktop Fiona 4 920 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  How can I change the uuid name of a file to his original file? MaddoxMB 2 934 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  Trouble with threading and reading variable from a different script Lembas 14 3,067 Apr-26-2023, 11:21 PM
Last Post: Lembas
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,113 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Reading a file JonWayn 3 1,101 Dec-30-2022, 10:18 AM
Last Post: ibreeden
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,130 Dec-15-2022, 04:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020