python - How to force every line to have the same # of tabs as the maximum length line -
I have a tab-delimited text, and I want to make tabs like the same number in each row have tab of the big number. For example,
a \ tB \ tC \ tD \ tF \ tG \ t Input file: Will be
a \ tB \ tC \ tD e \ t \ t \ tF \ tG \ t \ t I'm trying this is.
to import the Ayatr izip_longest import codecs inputf = sys.argv [1] outputf = sys.argv [2] as the F-open (inputf): data = izip_longest (* (* X.split ('\ t') for the line (* data) f), fillvalue = '\ t'): print line, offline = codecs.open (outputf, "w") But there is nothing in the output, although it prints things in the command window.
I hope this program does not print in these command windows (it seems that takes a lot of time)
and I hope the output file has the right output .
Try using the CSV module, like this
#! / Usr / bin / env pyt import CSV Hein import System import Ijit_long def Reed_aro (Inputfail) with open (Inputfail, 'RB') H: reader = CSV. Reader (H, quote = "Excel-tab") returns List (Reader) def write_rows (Output, rows): open (Output, as' Wb ') H: Writer = Csvkwriter (H, quote =' Excel -tab ') for the row in lines: writer.writerow (line) def show_file (outputfile): Open (output file,' R ') as H (): h.read (). Splitlines () Diif main (input file, Output): rows = Ptar_pojh (Input File) rows = ZIP (* (izip_longest (* rows, fill = ''))) Write_rows (Outputfile, rows) Show_file (Outputfile) if __name__ == ' __main__ ': inputfile = sys.argv [1] outputfile = sys.argv [2] key (inputfile, outputfile) with your input file:
< Code> ./in normally ~ / Downloads / input.txt ~ / Downloads / output.txt ['A \ TB \ TC \ tD', 'E \ t \ t \ t', 'F \ TG \ t \ t ']
Comments
Post a Comment