I assume you are wanting an output along the lines of the ASCII gridded XYZ format (or variation on this theme).
There are a few simple approaches to this depending on what licence and modules you have. What I would not do, though, is manually iterate over the raster to generate your XYZ file. You could do that but it will take time to write and debug and will most likely be slower than calling 'pre-coded' modules (which will be calling compiled code under the hood even from Python).
The first approach would be to convert the data to an XYZ ascii format. Here you have a whole heap of methods depending on taste:
- If you have 3D Analyst you could use Feature Class Z to ASCII from the Convert Toolbox.
- Use Raster to Point (Conversion) to convert your raster to points, add the X, Y columns to the attribute table and save the attribute table as a CSV (crude, but simple and effective!) making it either a headerless variant or not depending on your needs.
- Install GDAL and call GDAL_Translate from within a Python script. GDAL offers a couple of options that will give the data in an XYZ ASCII raster format - check the documentation for the flavour you prefer.
- This is not an exhaustive list of methods - there are others - it's just a few ideas of how you could do it in with tools usually readily available!
Secondly, you could sample your raster, this is pretty much what I think you wanted to do but I'd not bother writing my own script unless I had to. Instead, if you have Spatial Analyst, then use the Sample toolbox to create an XYZ table (I think it effectively comes out as ZXY but you can add a couple of steps to your process to reorder the columns). Save the output table as a CSV.
I've really just scratched the surface because you don't even need ArcGIS to do this. If you have GDAL and SciPy (both free) you can create a very fast approach of sampling the raster if you must roll-your-own method. I'd maybe use GDAL to read the raster to an array (in one go) and then write the array to a text file prepending the X and Y columns as you go (if you know raster origin then you can calculate the center of each cell as an offset). You can do this in Python.
Hopefully, this has given you a few ideas.