readfile - Skipping whitespaces when reading file python -


im working on long project read file saved in campus network system, when reading file, works if delete white spaces @ bottom of list, when leave them in(like prof wants) error, "invalid literal int() base 10: 'date'" ive tried few different options ignore white spaces none have worked- list of ive tried

 open("c:\\users\\brayd\onedrive\\documents\\2015homicidelog_final.txt") f_in:      lines = (line.rstrip() line in f_in)       lines = list(line line in lines if line)  line in file: if not line.strip(): print("it empty line")  open("fname.txt") file:     line in file:       if not line.strip():          file.close()   open file f_in: lines = list(line line in (l.strip() l in f_in) if line) 

nothing has worked, here use when deleting white spaces in file , works perfectly

file = open("c:\\users\\brayd\onedrive\\documents\\2015homicidelog_final.txt" , "r") lines=file.readlines()[1:] file.close() 

i've been working , trying work around whitespaces 12 hours , haven't had luck... ideas guys?

here text file looks like-

date   event #  time    victim name     v r/g   v age 150101 0685 2:03    anderson, kedral    bm  26 150103 0816 5:57    shines, kathryn     wf  54 150106 4417 22:06   norton, noella      hf  46 150107 4655 23:27   speidel, steven     wm  41 150110 1100 8:35    orozco, jose        hm  53 *blank spaces here*      *blank spaces here* *bsh^* 

for better example of program does, here full code

def dayofmurder(date): #function find day of murder     date = date%10000 #takes 10000 out leaving 2 digits year     month = date//100 #takes 100 out leaving 1-2 digits month     date= date %100 # mod 100 figure out date     day=date #day=date     monthlist = [0,31,59,90,120,151,181,212,243,273,304,334] #possible months through date ranges     daysofweek = ["sunday","monday","tuesday","wednesday", #list of days of week     "thursday","friday","saturday"]     startonday = 4 #start on 4th day (thursday) per txt file     startonday = monthlist[month-1]+(day-1)+startonday # start on day w/ days     startonday %= 7 #mod 7 find day of week     return daysofweek[startonday] #return day of week homicide on  daysoftheweek = ["sunday","monday","tuesday","wednesday", "thursday","friday","saturday"] #list of days of week printing in order   file = open("c:\\users\\brayd\onedrive\\documents\\2015homicidelog_final.txt" , "r") lines=file.readlines()[1:] file.close()  print("days homicides happened on:") dayofmurders = {"sunday": 0 ,"monday": 0,"tuesday": 0,"wednesday": 0, "thursday":0, "friday": 0,"saturday": 0} #list of days , start vaule of 0  #murders line in lines: #reads lines     value=line.split() #splits each value in line     listdays=(dayofmurder(int(value[0]))) #for every value in row     dayofmurders[listdays] = dayofmurders[listdays] + 1 #every time there     #occurance, add 1 total value in dayofmurders  v in daysoftheweek: #in order of value (s-m-t-w-th-f-s (from daysoftheweek      print(dayofmurders[v],"homicides happen on a", v)   #prints [v](value) of      #daysofmurders string " " , prints v (value) in daysoftheweek) print("----------------------------------",'\n', "number of homicides\ in hour block:") time = {"0:" : 0, "1:" : 0, "2:" : 0, "3:" : 0, "4:" : 0, "5:" : 0, "6:" : 0,         ##list of possible time's         "7:" : 0,"8:" : 0, "9:" : 0, "10" : 0, "11" : 0,"12" : 0, "13" : 0,            # " " hour possible         "14" : 0, "15" : 0,"16" : 0,  "17" : 0, "18" : 0, "19" : 0,"20" : 0,           # 0 value number of occurances         "21" : 0, "22" : 0, "23" : 0} line in lines:      #reads each line of file     value=line.split()  #splits each value in line     listdays=(value[2][0:2])  #moves index of line , grabs      #first 2 variables     time[listdays] = time[listdays] + 1  k,v in time.items():  #uses key , value in time dict     print(v,"homicides happened in",k,"hour block")  # print("----------------------------------",'\n', "races , occurances of hom\ idices") races = {"hf": 0 ,"hm": 0,"wf": 0,"wm": 0,"af":0, "bm": 0,"bf": 0, "am": 0}  #list of races , start value of 0 line in lines: #function find races in homicide file     value=line.split()     listdays=(value[5])     if listdays == "chunng": #if statement people have more than2      #names         listdays = (value[6]) #if have more 2 names, move          #next index slot , register race     elif listdays == "terrance": #same above         listdays = (value[6]) #same above     elif listdays == "lasunda": #same above         listdays = (value[6]) #same above     else:         listdays = (value[5]) #same above     races[listdays] = races[listdays] + 1 #for every occurance add's 1     #value  k,v in races.items(): #uses key , value in dictionary races     print(k,"=",v) #prints key , value in race dictionary  ages = { "0" : 0, "1" : 0, "2" : 0, "3" : 0, #list of possible ages , #         "4" : 0, "5" : 0, "6" : 0,"7" : 0,  #occurances         "8" : 0,"9" : 0}  line in lines:   #function find ages in homicide file     value = line.split()     listdays = (value[6][0])     if listdays == "a": #for people w/ 3 names, if index 6 = a/b/t(see race's)         listdays = (value[7][0]) # skip next index , use index 7     elif listdays == "b":         listdays = (value[7][0])     elif listdays == "t":         listdays = (value[7][0])     else:         listdays = (value[6][0])     ages[listdays] = ages[listdays]+ 1 #adds occurances  k,v in ages.items(): #uses key , value in dictionary ages     print(k, "=", v) #prints key , value in age dictionary  print("----------------------------------",'\n', "here graphs from\ data found above")  import pylab #importing pylab graphs bar_width = .75 x_values = [1,2,3,4,5,6,7] #range 1-7 y_values = [13,25,17,26,20,14,19] # data murder occurances, see above tlabel = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"] pylab.title("homicide occurenece day of week per homicides file") pylab.bar(x_values, y_values, width=bar_width, tick_label = tlabel, align =  'center' , color = 'b') pylab.show()  pylab.axes(aspect = 1) #used pylab example sheet values = [39, 11, 31, 6, 1, 2, 29, 15] #data race/gender see above pie_labels = ["bm", "bf", "hm", "hf", "am", "af", "wm", "wf"] color_list = ['purple', 'green', 'blue', 'cyan', 'yellow', 'maroon', 'red',               'white'] pylab.pie(values,autopct = '%1.f%%', labels = pie_labels, colors=color_list) pylab.title("pie chart showing racial , gender breakdown in homicides file") pylab.show()       bar_width = .5 #used pylab examples sheet (sets bar width) x_values = [0,1,2,3,4,5,6,7,8,9] #range 0-9 (0-9,10-19,20-29... ect) y_values = [4,7,27,41,4,15,7,6,2,5] # number of occurances per age tlabel = ["0-10", "11-20", "21-30", "31-40", "41-50", "51-60",           "61-70", "71-80", "81-90", "90+"] pylab.title("homicides per age categories in homocide file") pylab.bar(x_values, y_values, width=bar_width, tick_label = tlabel, align =  'center' , color = 'b') pylab.show()  bar_width = .3 #pylab example sheet(sets bar width) x_values = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23] #^number of hours possible murders y_values = [3,3,7,1,4,6,4,4,4,5,5,3,8,4,6,2,5,13,10,6,7,5,13,6] #occurances #of deaths per hour tlabel = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",           "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"] pylab.title("homicides per hour of clock in homicide file") pylab.bar(x_values, y_values, width=bar_width, tick_label = tlabel, align =  'center' , color = 'b') pylab.show() 

to remove spaces use:

yourstring = yourstring.replace(" ", "") 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -