String index out of range when reading data - python
For this problem, I have a separate txt file which contains a list of values down below:
Years+1900 Populationx106
0 1650
10 1750
20 1860
30 2070
40 2300
50 2560
60 3040
70 3710
80 4450
90 5280
100 6080
110 6870
For the problem I'm working on, I'm supposed to obtain that file and path name to then use to do calculations on with some functions I created. I have finished the functions I need to do, however I'm having an issue running it because I believe when doing the function it reads the "Years+1900 Populationx106" part first instead of the numbers below it.
Here's the code for my functions:
Input: year
Output: estimate of population for that year
def pop(year):
return 1436.53*((1.01395)**year)
# Input: data
# Return: the average error as per equation 18.
def error(data):
error=0
for i in data:
error +=(abs(i[1]-pop(i[0]))/i[1])
return 100*error/12
Here is the code I created to retrieve the data from my separate txt file:
def get_data(path,name):
with open("Assignment7/pop.txt", "r") as path:
path = open("Assignment7/pop.txt", "r")
name = path.read()
return name
The error I'm receiving is for the part below. It is an index error and it says the string index is out of range. I believe this is because it is reading the first part of the data in the pop.txt, how can I remove te first line in the pop.txt so that it only reads the numerical values I have?
error +=(abs(i[1]-pop(i[0]))/i[1])
I have tried changing the index values already, however it still says that my string index is out of range.
Let's assume you are correct and passing the first line of your text file to your function is breaking it.
You can "throw away" the first line of the text file by reading it as a single line (but doing nothing with it) and then reading the data you actually want like this..
def get_data(path,name):
with open("Assignment7/pop.txt", "r") as path:
path = open("Assignment7/pop.txt", "r")
header=path.readline() #Read "Header line", but don't use it
name = path.read() #Read subsequent lines as the data you want
return name
I suspect that you’ve simply read the entire file as one string. Therefore each element, i, is a single character and has no dimensionality. You’ll need to either parse the file by the new line character to split it into by line (and likely again to get it the two separate columns).
Python String Split will be useful for the that.
You’re correct that the first line will pose issues, but this can be removed by using a path.readline() call as Richard said.
Related
Index out of bounds for a 2D Array?
I am having a 2D List and i am trying to retrieve a column with the index spcified as a parameter (type : IntEnum). I get the index out of bounds error when trying to retrieve any column other then the one at index 0. Enum: class Column(IntEnum): ROAD = 0 SECTION = 1 FROM = 2 TO = 3 TIMESTAMP = 4 VFLOW=5 class TrafficData: data=[[]] Below are member methods of TrafficData Reading from file and storing the matrix: def __init__(self,file): self.data=[[word for word in line.split('\t')]for line in file.readlines()[1:]] Retrieve-ing the desired column: def getColumn(self,columnName): return [line[columnName] for line in self.data] Call: ) column1 = traficdata.getColumn(columnName=Column.ROAD) `column2 = traficdata.getColumn(columnName=Column.FROM)` //error `column3 = traficdata.getColumn(columnName=Column.TO)` //error I attached a picture with the data after __init__ processing: [
I tested the code that you provided above, and didn't see any issues. That leads me to believe that there might be something wrong with the data that you have in the file. Could you paste the file data? (the tab delimited data) UPDATE - I found the issue - as suspected, it was a data issue (there is a minor code update involved too). Make the following changes - 1) When opening the file use the appropriate encoding, I used utf-16. 2) At the end of the data file that you shared, it contains the text - "(72413 row(s) affected)" along with a couple of new line characters. So, you have 2 options, either manually cleanup the data file, or update the code to ignore the "(72413 row(s) affected)" & "\n" characters. Hope that helps.
Efficiently Find Partial String Match --> Values Starting From List of Values in 5 GB file with Python
I have a 5GB file of businesses and I'm trying to extract all the businesses that whose business type codes (SNACODE) start with the SNACODE corresponding to grocery stores. For example, SNACODEs for some businesses could be 42443013, 44511003, 44419041, 44512001, 44522004 and I want all businesses whose codes start with my list of grocery SNACODES codes = [4451,4452,447,772,45299,45291,45212]. In this case, I'd get the rows for 44511003, 44512001, and 44522004 Based on what I googled, the most efficient way to read in the file seemed to be one row at a time (if not the SQL route). I then used a for loop and checked if my SNACODE column started with any of my codes (which probably was a bad idea but the only way I could get to work). I have no idea how many rows are in the file, but there are 84 columns. My computer was running for so long that I asked a friend who said it should only take 10-20 min to complete this task. My friend edited the code but I think he misunderstood what I was trying to do because his result returns nothing. I am now trying to find a more efficient method than re-doing my 9.5 hours and having my laptop run for an unknown amount of time. The closest thing I've been able to find is most efficient way to find partial string matches in large file of strings (python), but it doesn't seem like what I was looking for. Questions: What's the best way to do this? How long should this take? Is there any way that I can start where I stopped? (I have no idea how many rows of my 5gb file I read, but I have the last saved line of data--is there a fast/easy way to find the line corresponding to a unique ID in the file without having to read each line?) This is what I tried -- in 9.5 hours it outputted a 72MB file (200k+ rows) of grocery stores codes = [4451,4452,447,772,45299,45291,45212] #codes for grocery stores for df in pd.read_csv('infogroup_bus_2010.csv',sep=',', chunksize=1): data = np.asarray(df) data = pd.DataFrame(data, columns = headers) for code in codes: if np.char.startswith(str(data["SNACODE"][0]), str(code)): with open("grocery.csv", "a") as myfile: data.to_csv(myfile, header = False) print code break #break code for loop if match grocery.to_csv("grocery.csv", sep = '\t') This is what my friend edited it to. I'm pretty sure the x = df[df.SNACODE.isin(codes)] is only matching perfect matches, and thus returning nothing. codes = [4451,4452,447,772,45299,45291,45212] matched = [] for df in pd.read_csv('infogroup_bus_2010.csv',sep=',', chunksize=1024*1024, dtype = str, low_memory=False): x = df[df.SNACODE.isin(codes)] if len(x): matched.append(x) print "Processed chunk and found {} matches".format(len(x)) output = pd.concat(matched, axis=0) output.to_csv("grocery.csv", index = False) Thanks!
To increase speed you could pre-build a single regexp matching the lines you need and the read the raw file lines (no csv parsing) and check them with the regexp... codes = [4451,4452,447,772,45299,45291,45212] col_number = 4 # Column number of SNACODE expr = re.compile("[^,]*," * col_num + "|".join(map(str, codes)) + ".*") for L in open('infogroup_bus_2010.csv'): if expr.match(L): print L Note that this is just a simple sketch as no escaping is considered... if the SNACODE column is not the first one and preceding fields may contain a comma you need a more sophisticated regexp like: ... '([^"][^,]*,|"([^"]|"")*",)' * col_num + ... that ignores commas inside double-quotes
You can probably make your pandas solution much faster: codes = [4451, 4452, 447, 772, 45299, 45291, 45212] codes = [str(code) for code in codes] sna = pd.read_csv('infogroup_bus_2010.csv', usecols=['SNACODE'], chunksize=int(1e6), dtype={'SNACODE': str}) with open('grocery.csv', 'w') as fout: for chunk in sna: for code in chunk['SNACODE']: for target_code in codes: if code.startswith(target_code): fout.write('{}\n'.format(code)) Read only the needed column with usecols=['SNACODE']. You can adjust the chunk size with chunksize=int(1e6). Depending on your RAM you can likely make it much bigger.
Script skips second for loop when reading a file
I am trying to read a log file and compare certain values against preset thresholds. My code manages to log the raw data from with the first for loop in my function. I have added print statements to try and figure out what was going on and I've managed to deduce that my second for loop never "happens". This is my code: def smartTest(log, passed_file): # Threshold values based on averages, subject to change if need be RRER = 5 SER = 5 OU = 5 UDMA = 5 MZER = 5 datafile = passed_file # Log the raw data log.write('=== LOGGING RAW DATA FROM SMART TEST===\r\n') for line in datafile: log.write(line) log.write('=== END OF RAW DATA===\r\n') print 'Checking SMART parameters...', log.write('=== VERIFYING SMART PARAMETERS ===\r\n') for line in datafile: if 'Raw_Read_Error_Rate' in line: line = line.split() if int(line[9]) < RRER and datafile == 'diskOne.txt': log.write("Raw_Read_Error_Rate SMART parameter is: %s. Value under threshold. DISK ONE OK!\r\n" %int(line[9])) elif int(line[9]) < RRER and datafile == 'diskTwo.txt': log.write("Raw_Read_Error_Rate SMART parameter is: %s. Value under threshold. DISK TWO OK!\r\n" %int(line[9])) else: print 'FAILED' log.write("WARNING: Raw_Read_Error_Rate SMART parameter is: %s. Value over threshold!\r\n" %int(line[9])) rcode = mbox(u'Attention!', u'One or more hardrives may need replacement.', 0x30) This is how I am calling this function: dataOne = diskOne() smartTest(log, dataOne) print 'Disk One Done' diskOne() looks like this: def diskOne(): if os.path.exists(r"C:\Dejero\HDD Guardian 0.6.1\Smartctl"): os.chdir(r"C:\Dejero\HDD Guardian 0.6.1\Smartctl") os.system("Smartctl -a /dev/csmi0,0 > C:\Dejero\Installation-Scripts\diskOne.txt") # Store file in variable os.chdir(r"C:\Dejero\Installation-Scripts") datafile = open('diskOne.txt', 'rb') return datafile else: log.write('Smart utility not found.\r\n') I have tried googling similar issues to mine and have found none. I tried moving my first for loop into diskOne() but the same issue occurs. There is no syntax error and I am just not able to see the issue at this point.
It is not skipping your second loop. You need to seek the position back. This is because after reading the file, the file offset will be placed at the end of the file, so you will need to put it back at the start. This can be done easily by adding a line datafile.seek(0); Before the second loop. Ref: Documentation
Python: How to extract string from text file to use as data
this is my first time writing a python script and I'm having some trouble getting started. Let's say I have a txt file named Test.txt that contains this information. x y z Type of atom ATOM 1 C1 GLN D 10 26.395 3.904 4.923 C ATOM 2 O1 GLN D 10 26.431 2.638 5.002 O ATOM 3 O2 GLN D 10 26.085 4.471 3.796 O ATOM 4 C2 GLN D 10 26.642 4.743 6.148 C What I want to do is eventually write a script that will find the center of mass of these three atoms. So basically I want to sum up all of the x values in that txt file with each number multiplied by a given value depending on the type of atom. I know I need to define the positions for each x-value, but I'm having trouble with figuring out how to make these x-values be represented as numbers instead of txt from a string. I have to keep in mind that I'll need to multiply these numbers by the type of atom, so I need a way to keep them defined for each atom type. Can anyone push me in the right direction?
mass_dictionary = {'C':12.0107, 'O':15.999 #Others...? } # If your files are this structured, you can just # hardcode some column assumptions. coords_idxs = [6,7,8] type_idx = 9 # Open file, get lines, close file. # Probably prudent to add try-except here for bad file names. f_open = open("Test.txt",'r') lines = f_open.readlines() f_open.close() # Initialize an array to hold needed intermediate data. output_coms = []; total_mass = 0.0; # Loop through the lines of the file. for line in lines: # Split the line on white space. line_stuff = line.split() # If the line is empty or fails to start with 'ATOM', skip it. if (not line_stuff) or (not line_stuff[0]=='ATOM'): pass # Otherwise, append the mass-weighted coordinates to a list and increment total mass. else: output_coms.append([mass_dictionary[line_stuff[type_idx]]*float(line_stuff[i]) for i in coords_idxs]) total_mass = total_mass + mass_dictionary[line_stuff[type_idx]] # After getting all the data, finish off the averages. avg_x, avg_y, avg_z = tuple(map( lambda x: (1.0/total_mass)*sum(x), [[elem[i] for elem in output_coms] for i in [0,1,2]])) # A lot of this will be better with NumPy arrays if you'll be using this often or on # larger files. Python Pandas might be an even better option if you want to just # store the file data and play with it in Python.
Basically using the open function in python you can open any file. So you can do something as follows: --- the following snippet is not a solution to the whole problem but an approach. def read_file(): f = open("filename", 'r') for line in f: line_list = line.split() .... .... f.close() From this point on you have a nice setup of what you can do with these values. Basically the second line just opens the file for reading. The third line define a for loop that reads the file one line at a time and each line goes into the line variable. The last line in that snippet basically breaks the string --at every whitepsace -- into an list. So line_list[0] will be the value on your first column and so forth. From this point if you have any programming experience you can just use if statements and such to get the logic that you want. ** Also keep in mind that the type of values stored in that list will all be string so if you want to perform any arithmetic operations such as adding you have to be careful. * Edited for syntax correction
If you have pandas installed, checkout the read_fwf function that imports a fixed-width file and creates a DataFrame (2-d tabular data structure). It'll save you lines of code on import and also give you a lot of data munging functionality if you want to do any additional data manipulations.
Unexpected whitespace in python generated strings
I am using Python to generate an ASCII file composed of very long lines. This is one example line (let's say line 100 in the file, '[...]' are added by me to shorten the line): {6 1,14 1,[...],264 1,270 2,274 2,[...],478 1,479 8,485 1,[...]} If I open the ASCII file that I generated with ipython: f = open('myfile','r') print repr(f.readlines()[99]) I do obtain the expected line printed correctly ('[...]' are added by me to shorten the line): '{6 1,14 1,[...],264 1,270 2,274 2,[...],478 1,479 8,485 1,[...]}\n' On the contrary, if I open this file with the program that is suppose to read it, it will generate an exception, complaining about an unexpected pair after 478 1. So I tried to open the file with vim. Still vim shows no problem, but if I copy the line as printed by vim and paste it in another text editor (in my case TextMate), this is the line that I obtain ('[...]' are added by me to shorten the line): {6 1,14 1,[...],264 1,270 2,274 2,[...],478 1,4 79 8,485 1,[...]} This line indeed has a problem after the pair 478 1. I tried to generate my lines in different ways (concatenating, with cStringIO, ...), but I always obtain this result. When using the cStringIO, for example, the lines are generated as in the following (even though I tried to change this, as well, with no luck): def _construct_arff(self,attributes,header,data_rows): """Create the string representation of a Weka ARFF file. *attributes* is a dictionary with attribute_name:attribute_type (e.g., 'num_of_days':'NUMERIC') *header* is a list of the attributes sorted (e.g., ['age','name','num_of_days']) *data_rows* is a list of lists with the values, sorted as in the header (e.g., [ [88,'John',465],[77,'Bob',223]]""" arff_str = cStringIO.StringIO() arff_str.write('#relation %s\n' % self.relation_name) for idx,att_name in enumerate(header): try: name = att_name.replace("\\","\\\\").replace("'","\\'") arff_str.write("#attribute '%s' %s\n" % (name,attributes[att_name])) except UnicodeEncodeError: arff_str.write('#attribute unicode_err_%s %s\n' % (idx,attributes[att_name])) arff_str.write('#data\n') for data_row in data_rows: row = [] for att_idx,att_name in enumerate(header): att_type = attributes[att_name] value = data_row[att_idx] # numeric attributes can be sparse: None and zeros are not written if ((not att_type == constants.ARRF_NUMERIC) or not ((value == None) or value == 0)): row.append('%s %s' % (att_idx,value)) arff_str.write('{' + (','.join(row)) + '}\n') return arff_str.getvalue() UPDATE: As you can see from the code above, the function transforms a given set of data to a special arff file format. I noticed that one of the attributes I was creating contained numbers as strings (e.g., '1', instead of 1). By forcing these numbers into integers: features[name] = int(value) I recreated the arff file successfully. However I don't see how this, which is a value, can have an impact on the formatting of *att_idx*, which is always an integer, as also pointed out by #JohnMachin and #gnibbler (thanks for your answers, btw). So, even if my code runs now, I still don't see why this happens. How can the value, if not properly transformed into int, influence the formatting of something else? This file contains the wrongly formatted version.
The built-in function repr is your friend. It will show you unambiguously what you have in your file. Do this: f = open('myfile','r') print repr(f.readlines()[99]) and edit your question to show the result. Update: As to how it got there, it is impossible to tell, because it cannot have been generated by the code that you showed. The value 37 should be a value of att_idx which comes from enumerate() and so must be an int. You are formatting this int with %s ... 37 can't become 3rubbish7. Also that should generate att_idx in order 0, 1, etc etc but you are missing many values and there is nothing conditional inside your loop. Please show us the code that you actually ran. Update: And again, this code won't run: for idx,att_name in enumerate(header): arff_str.write("#attribute '%s' %s\n" % (name,attributes[att_name])) because name is not defined; you probably mean att_name. Perhaps we can short-circuit all this stuffing about: post a copy of your output file (zipped if it's huge) on the web somewhere so that we can see for ourselves what might be disturbing its consumers. Please do edit your question to say which line(s) exhibits(s) the problem. By the way, you say some of the data is string rather than integer, and the problem goes away if you coerce the data to int by doing features[name] = int(value) ... what is 'features'?? What is 'name'?? Are any of those strings unicode instead of str? Update 2 (after bad file posted on net) No info supplied on which line(s) exhibits(s) the problem. As it turned out, no lines exhibited the described problem with attribute 479. I wrote this checking script: import re, sys # sample data line: # {40 1,101 3,319 2,375 2,525 2,530 bug} # Looks like all data lines end in ",530 bug}" or ",530 other}" pattern1 = r"\{(?:\d+ \d+,)*\d+ \w+\}$" matcher1 = re.compile(pattern1).match pattern2 = r"\{(?:\d+ \d+,)*" matcher2 = re.compile(pattern2).match bad_atts = re.compile(r"\D\d+\s+\W").findall got_data = False for lino, line in enumerate(open(sys.argv[1], "r"), 1): if not got_data: got_data = line.startswith('#data') continue if not matcher1(line): print print lino, repr(line) m = matcher2(line) if m: print "OK up to offset", m.end() print bad_atts(line) Sample output (wrapped at column 80): 581 '{2 1,7 1,9 1,12 1,13 1,14 1,15 1,16 1,17 1,18 1,21 1,22 1,24 1,25 1,26 1,27 1,29 1,32 1,33 1,36 1,39 1,40 1,44 1,48 1,49 1,50 1,54 1,57 1,58 1,60 1,67 1,68 1,69 1,71 1,74 1,75 1,76 1,77 1,80 1,88 1,93 1,101 ,103 6,104 2,109 20,110 3,11 2 2,114 1,119 17,120 4,124 39,128 5,137 1,138 1,139 1,162 1,168 1,172 18,175 1,1 76 6,179 1,180 1,181 2,185 2,187 9,188 8,190 1,193 1,195 2,196 4,197 1,199 3,201 3,202 4,203 5,206 1,207 2,208 1,210 2,211 1,212 5,213 1,215 2,216 3,218 2,220 2 ,221 3,225 8,226 1,233 1,241 4,242 1,248 5,254 2,255 1,257 4,258 4,260 1,266 1,2 68 1,269 3,270 2,271 5,273 1,276 1,277 1,280 1,282 1,283 11,285 1,288 1,289 1,29 6 8,298 1,299 1,303 1,304 11,306 5,308 1,309 8,310 1,315 3,316 1,319 11,320 5,32 1 11,322 2,329 1,342 2,345 1,349 1,353 2,355 2,358 3,359 1,362 1,367 2,368 1,369 1,373 2,375 9,377 1,381 4,382 1,383 3,387 1,388 5,395 2,397 2,400 1,401 7,407 2 ,412 1,416 1,419 2,421 2,422 1,425 2,427 1,431 1,433 7,434 1,435 1,436 2,440 1,4 49 1,454 2,455 1,460 3,461 1,463 1,467 1,470 1,471 2,472 7,477 2,478 11,479 31,4 82 6,485 7,487 1,490 2,492 16,494 2,495 1,497 1,499 1,501 1,502 1,503 1,504 11,5 06 3,510 2,515 1,516 2,517 3,518 1,522 4,523 2,524 1,525 4,527 2,528 7,529 3,530 bug}\n' OK up to offset 203 [',101 ,'] 709 '{101 ,124 2,184 1,188 1,333 1,492 3,500 4,530 bug}\n' OK up to offset 1 ['{101 ,'] So it looks like the attribute with att_idx == 101 can sometimes contain the empty string ''. You need to sort out how this attribute is to be treated. It would help your thinking if you unwound this Byzantine code: if ((not att_type == constants.ARRF_NUMERIC) or not ((value == None) or value == 0)): Aside: that "expletive deleted" code won't run; it should be ARFF, not ARRF into: if value or att_type != constants.ARFF_NUMERIC: or maybe just if value: which will filter out all of None, 0, and "". Note that att_idx == 101 corresponds to the attribute "priority" which is given a STRING type in the ARFF file header: [line 103] #attribute 'priority' STRING By the way, your statement about features[name] = int(value) "fixing" the problem is very suspicious; int("") raises an exception. It may help you to read the warning at the end of this wiki section about sparse ARFF files.