I need to write a text document that has a specific number of columns (in this case there are always 16). The way I found to make this happen was: check which is the len of the array (list) where the values I want are and divide by 16, getting the number of rows enough to equate to 16 columns precisely. Surely there is a much more correct way to optimize this process. Another question I take advantage of to ask is (because I really don't know how to do it): how to write in 16 columns and always leave with an indentation equivalent to the largest number of that array (i.e.: 8500 = 4 characters = 4 spaces). Something like this:
Here is my code:
f = open('Questions.txt','r') my_list = [line.split(',')[0] for line in f] ff = open('Questions_new.txt','w') i=0 x=len(my_list) rows = int(round(len(my_list)//16)) for i in range(rows): row = my_list [i::rows] ff.write(' '.join(str(x) for x in row)+'\n') The use of line.split is because I am extracting from a text document (example below) all characters of a text document until a comma is found:
1213,4214 12312
13,1231 123
45,343
and he just keeps the information for me:
1213
13
45
If someone could help I would be thankful.
https://stackoverflow.com/questions/65756044/write-file-with-specific-number-of-columns January 17, 2021 at 08:11AM
没有评论:
发表评论