Writing out 3D volume to text file is a bad idea (large file, slow to read/write, potential data loss due to limited precision), but here is a complete example just to show how easy it is to do:
# Get some image data
import SampleData
volumeNode=SampleData.SampleDataLogic().downloadMRHead()
# Get voxels as numpy array
volumeArray=slicer.util.arrayFromVolume(volumeNode)
# Write voxels to text file
import numpy as np
with file('c:/tmp/test.txt', 'w') as outfile:
for data_slice in volumeArray:
np.savetxt(outfile, data_slice, fmt='%-7.2f')
If you want to write in a different format then search for Python code examples on the web.