[Numpy-discussion] how do I specify maximum line length when using savetxt?
Wed Dec 5 18:30:44 CST 2012
assuming savetxt does not support it,
I modified a bit of code I had to do what I think you need ONLY works
for a 1D array and wrapped it into a function that writes in properly
formatted columns. I didn't really test it other than what is there. I
"dressed" it like savetxt but the glaring difference is that it goes off
significant digits as opposed to format.
def mysavetxt_forvector(fname, x, sigDigs, delimiter, newline,
padSize = sigDigs + 6 #How many characters per number including
fmt = str(padSize) + '.' + str(sigDigs) + 'g' #e.g. 13.7g'
asTxtLst = map(lambda val: format(val, fmt), a) #from array to list
of formatted strings
#how many cols max ?
cols = maxCharsPLine/(padSize + len(delimiter))
#write to file
size = len(asTxtLst)
col = 0
f = open(fname, fmode)
while col < size:
col += cols
a = numpy.ones(34, dtype='float64') * 7./3.
a = 123564234.0002345
a = 1
a = -123564234.0002345
a = -.00000000000023453456345
sigDigs = 7
maxCharsPLine = 80
delimiter = ','
newline = '\n'
fname = 'temp.out'
mysavetxt_forvector(fname, a, sigDigs, delimiter, newline, maxCharsPLine)
#append on this one
maxCharsPLine = 33
mysavetxt_forvector(fname, a, sigDigs, delimiter, newline,
On 05/12/2012 4:40 PM, Mark Bakker wrote:
> I guess I wasn't explicit enough.
> Say I have an array with 100 numbers and I want to write it to a file
> with 6 numbers on each line (and hence, only 4 on the last line).
> Can I use savetxt to do that?
> What other easy tool does numpy have to do that?
> On 5. des. 2012, at 22:35, Mark Bakker wrote:
>> Hello List,
>> I want to write a large array to file, and each line can only be 80
>> characters long.
>> Can I use savetxt to do that? Where would I specify the maximum line length?
> If you specify the format, %10.3f for instance, you will know the max
> line length if you also know the array shape.
>> Or is there a better way to do this?
> Probably 1000 ways to accomplish the same thing out there, sure.
> NumPy-Discussion mailing list
More information about the NumPy-Discussion