python - Only writing to a file certain columns of a line (seperated by spaces) -


i reading log line line. trying print columns of line. bash script use awk , $ seperate it. however, cant figure out how python. tried using split, doesnt want.

my code right now:

for line in file:     if stored_procs_begin in line:         log.write(line)     elif stored_procs_finished in line:         log.write(line)     elif stored_task_begin in line:         log.write(line)     elif stored_task_finished in line:         log.write(line)     elif actuate_report_schedule in line:         break 

so when trying format line being passed write().

example of want:

date time info junk1 junk2 name => date time info name

edit: got idea split , extract fields want , them join them together.. there has better what.

you can split line words using split(), that's right. can index columns want have in output:

line = 'date time info junk1 junk2 name' parts = line.split() parts_i_want = parts[0:3] + parts[5:6] print ' '.join(parts_i_want) 

if want remove columns, can use del:

line = 'date time info junk1 junk2 name' parts = line.split() del parts[4]  # junk2 del parts[3]  # junk1 print ' '.join(parts) 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -