views:

86

answers:

3

I have a bit of code that runs through a dictionary and outputs the values from it in a CSV format. Strangely I'm getting a couple of blank lines where all the output of all of the dictionary entries is blank. I've read the code and can't understand has anything except lines with commas can be output. The blank line should have values in it, so extra \n is not the cause. Can anyone advise why I'd be getting blank lines? Other times I run the missing line appears.

Missing line:

6415, 6469, -4.60, clerical, 2, ,,,joe,030193027org,joelj,030155640dup

Using python 2.6.5

Bit of code:

      tfile = file(path, 'w')
      tfile.write('Rec_ID_A, Rec_ID_B, Weight, Assigned, Run, By, On, Comment\n')
      rec_num_a = 0

      while (rec_num_a <= max_rec_num_a):
        try:
            value = self.dict['DA'+str(rec_num_a)]
        except:
            value = [0,0,0,'rejected']
        if (value[3]!='rejected'):
            weightValue = "%0.2f" % value[2]
            line = value[0][1:] + ', ' + value[1][1:] + ', ' + weightValue \
                             + ', ' + str(value[3]) + ', ' + str(value[4]) 
            if (len(value)>5):
                line = line + ', ' + value[5] + ',' + value[6] + ',' + value[7]
            (a_pkey, b_pkey) = self.derive_pkeys(value)
            line = line + a_pkey + b_pkey
             tfile.write( line + '\n')
        rec_num_a +=1            

Sample output

6388, 2187, 76.50, clerical, 1, ,,,cameron,030187639org,cameron,030187639org
6398, 2103, 70.79, clerical, 1, ,,,caleb,030189225org,caldb,030189225dup
6402, 2205, 1.64, clerical, 2, ,,,jenna,030190334org,cameron,020305169dup
6409, 7892, 79.09, clerical, 1, ,,,liam,030191863org,liam,030191863org

6416, 11519, 79.09, clerical, 1, ,,,thomas,030193156org,thomas,030193156org
6417, 8854, 6.10, clerical, 2, ,,,ruby,030193713org,mia,020160397org
6421, 2864, -0.84, clerical, 2, ,,,kristin,030194394org,connou,020023478dup
6423, 413, 75.63, clerical, 1, ,,,adrian,030194795org,adriah,030194795dup
+3  A: 

Why are you not using Python's built-in csv module?

Then, what does self.derive_pkeys(value) do? Could it be that b_pkey sometimes ends with \n?

Tim Pietzcker
+1  A: 

without seeing the source data it is hard to tell, but I could speculate that your data has some stray \n characters in it, like in b_pkey . You could try and do a .strip() on that value to make sure it is clean.

fuzzy lollipop
That would be `.strip()`
Ian Bicking
+1  A: 

(1) Please say precisely what a "blank line" is -- contains only a newline? contains one or more spaces? Other whitespace characters?

(2) How did you determine the answer to Q1? ["looked at in a text editor" is not a good answer, nor is "printed it to my terminal and eyeballed it"; try print repr(line)]

(3) How have you determined that the "missing data" is actually in the input dictionary?

(4) Some runs work, some don't ... so what else is different? From what is the dict populated? A multiuser database? A static file?

John Machin