0

Python: reduce some column in TXT by regexp

Dear all, I try to read TXT file, change the lines and write a result in a new TXT file but the delimiter is "" how I could find \t""\t""\t""\t and replace with \t""\t with open( dat, "r", encoding="ANSI" ) as infile: with open( outdat, 'w' ) as outfile: line=infile.readline() cnt=1 while line: print( "Line {}: {}".format( cnt, line.strip() ) ) line=infile.readline() # ---------------------------------- # here I try to find and replace, but do not working # re.findall(r"^\d",line): # ---------------------------------- cnt+=1 outfile.write(line) outfile.close() infile.close() My sourse like that: "" "" "" "" "" "" "Interval: 01/01/17 00:00:00 - 31/01/17 23:59:59" "" "" "" "" "" "" "" "" "" "" "Trunk: 1" "" "" "" "" "" "" "End date/time" "" "Duration" "Phone number" "Ext." "" "" "Pin" "Pulses" "Raw charge" "Charge" "Type" "" "Trunk group: 1" "" "" "" "" "" "" "" "" "" "" "" "Circuit: 1" "" "" "" "" "" "" "" "" "" "" "03/01/17 11:04:00" "" "49s" "052648227" "331" "" "" "" "0" "$0.1222" "$0.1467" "7" "03/01/17 16:33:00" "" "5m,7s" "0899331694#" "339" "" "" "" "0" "$1.0643" "$1.2771" "7"

6th Nov 2017, 2:11 PM
Radostina Stefanova
Radostina Stefanova - avatar
2 ответов
+ 2
That's a tab-delimited file. The inclusion of "" where they aren't necessary is a safe (I guess) -- but unfortunate mixing -- of comma- and tab-separated-values (mixed philosophies). Still, I understand you're trying to solve the problem with regex. Is that required? Are you allowed to import csv? https://docs.python.org/3/library/csv.html If yes, I'd deal with the header and data separately. If no, I'll continue (or hopefully someone chimes in)
6th Nov 2017, 3:00 PM
Kirk Schafer
Kirk Schafer - avatar
+ 1
till now I use code below to locate rows with dates line_ckeck1 = re.match("\"\d",line) if line_ckeck1: print(str(cnt), " nameren s data") but for columns i will try with CSV Thank you!!!
6th Nov 2017, 8:50 PM
Radostina Stefanova
Radostina Stefanova - avatar