On Wed, 6 Apr 2016 07:49 am, Muhammad Ali wrote: > Would any one paste here some reference/sample python code for such > extraction of data into text file?
column1 = ['aa', 'bb', 'cc', 'dd', 'ee', 'ff'] # strings column2 = [97, 105, 16, 4, 48, 274] # ints column3 = [12.345, 7.821, -4.034, 19.034, 23.0, 42.0] # floats # Open the file for writing. It will be closed # automatically at the end of the block. with open("myfile.dat", "w") as f: for t in zip(column1, column2, column3): line = "%-9s %-9d %-9f\n" % t print(line, end='') f.write(line) The function zip(...) combines one element from each argument into a tuple of three values, like t = ('aa', 97, 12.345) t = ('bb', 105, 7.821) etc. The string formatting line: line = "%-9s %-9d %-9f\n" % t builds a new string. The code %s takes any object, including strings, and inserts it into the new string. %d requires an int, and %f requires a float. The \n at the end inserts a newline. The -9 inside the formatting codes sets the width and justification: %-9d formats an integer in nine columns, justified on the left, while %9d justifies to the right: py> print("%-9d." % 23) 23 . py> print("%9d." % 23) 23. Finally, the line is printed so you can see what it looks like, and written to the file. -- Steven -- https://mail.python.org/mailman/listinfo/python-list