Friday 15 August 2014

python - Correctly formatting data -



python - Correctly formatting data -

i have list of strings read file, convert them integers , strip them list, doing shown below

def reading_ppm(file_name): f = open (file_name) setting = f.readline().splitlines() comment = f.readline().splitlines() size_x, size_y = f.readline().split() pixel_max = f.readline().splitlines() orig_data = f.read().split() homecoming size_x,size_y,pixel_max, orig_data info = map(int, orig_data) info = str(data).strip('[]')

when write info new file get:

255, 255, 255, 255, 255, 255, 255, 255, 255,

however want

255 255 255 255 255 255 255 255 255 255 255 255 255 255

how can turn long string, ints appear on new line rather in 1 ?

thanks

here me writing file

def writting_ppm(ppm_file,size_x,size_y,maxval,data): colour = 'p3' print size_x print size_y # maxval = str(maxval).strip('['']') maxval = 255 # info = str(data).strip('[]') # print info open(ppm_file, "w") text_file: text_file.write(colour + "\n" + "\n" +str(size_x) + " " + str(size_y) + "\n" + str(maxval) +"\n" + (data) )

i trying implement loop :

with open(ppm_file, "w") text_file: text_file.write(colour + "\n" + "\n" +str(size_x) + " " + str(size_y) + "\n" + str(maxval) +"\n") count = 0 while count < len(data): text_file.write(data[count] + "\n") count = count + 1

but getting errors, the right way ?

you should utilize for loop , don't hack strip []. this:

def writting_ppm(ppm_file,size_x,size_y,maxval,data): colour = 'p3' print size_x print size_y # leave info list maxval = max(maxval) # utilize max max int in list open(ppm_file, "w") text_file: text_file.write(colour + "\n" + "\n" +str(size_x) + " " + str(size_y) + "\n" + str(maxval) +"\n") each in data: text_file.write(str(each)+'\n')

python

No comments:

Post a Comment