1

I wrote a Python code with the file.write command, so my calculated values are all written into a file. When prompted to open the file, I selected Microsoft Excel as the application to open it.

Here is the code:

for i in range (0,len(result)-1):  
...  file.write("%s " % result[i][0][0])     
...  file.write("%s \n" % result[i][0][1])
...  branch = result[i]
...  NumPoints = len(branch)  
...  for j in range (biggestHWS, NumPoints - biggestHWS):
...      HWS = biggestHWS # reset HWS
...      file.write("%s " % result[i][0][0])   
...      file.write("%s " % result[i][0][1])
...      file.write("%s \n" % j)
...      while HWS > 0:  
...              length = HWS * 2 * 10   
...              d = math.sqrt((branch[j-HWS][3] - branch[j+HWS][3]) ** 2 + (branch[j-HWS][4] - branch[j+HWS][4]) ** 2)
...              s = length / d
...              file.write("%s " % s)   
...              HWS = HWS - 1   
...      file.write(" \n")   
file.close()    

The values show up in the Excel spreadsheet but are not in the cells - it looks like they are just printed on top of the cells but not inside the cells. I am a beginner Python programmer, so I'm not sure what's going on.

Is there a way to correctly open the file so that the values are in Excel cells, or do I have to write the data into an Excel spreadsheet in my code?

whent1991
  • 227
  • 1
  • 4
  • 9
  • 1
    Please show your Python code as well as a sample of the output (say if you open it in Notepad instead of Excel). Also what have you named a file? You probably need a file extension that correlates with the format you are using. What you are probably seeing is all the data jammed into the first column, which makes me think you didn't print to .csv. – sunny Jul 20 '15 at 17:36
  • I added the code to my question. I'm unable to upload an image of the file in Notepad (not enough reputation). – whent1991 Jul 20 '15 at 17:45
  • I don't see any formatting in your code. Is this meant to be csv (comma separated value), or tab separated value or something else? If this is a numpy array, you can use something like this question: http://stackoverflow.com/questions/6081008/dump-a-numpy-array-into-a-csv-file or if you have a Pandas dataframe you can just use dataframe.tocsv("filename.csv"). Excel can't magically know what file format you want. – sunny Jul 20 '15 at 17:50

1 Answers1

2

If you want to output an Excel file from a Python script, you should probably use a module such as Xlsxwriter or xlwt. Otherwise you will need to write your data as a csv file or something and import it into Excel.

jwinterm
  • 334
  • 3
  • 11
  • Is it possible to write my finished file to a csv file? Or do I need to directly write my calculated values to a csv file? – whent1991 Jul 20 '15 at 18:21
  • A csv file is just a text file, where the fields are separated by commas (csv = comma separated variable), so it looks like currently you are writing a space separated variable file, and if you just change your spaces to commas, then you will have a comma separated variable file, and I think if you make the file extension '.csv' then Excel will try to open it and separate fields by the commas. Otherwise you'll have to use the Data-->Import feature in Excel and manually tell Excel how the fields are separated. – jwinterm Jul 20 '15 at 18:29