python - Creating lists from a non-csv .txt file; output to new .txt based on variable conditions -
i have 2 quick python questions. first experience python; have no programming experience whatsoever apologize - basic question , know should easy. did search existing answers not find helped.
basically, have .txt file keeps 31 variables. there header @ top category names, , following lines entries. looks this:
date time parameter3 ... parameter31 d1 t1 p3_1 ... p31_1 d2 t2 p3_2 ... p31_2 etc.
all of parameters numerical, except parameter6, either string of 3 letters or if empty "..."
the .txt file not comma-separated; 1 3 whitespaces separate each value.
i trying import text file python, create list out of each line, , have python check parameter7 , parameter9. if parameters 7 <30 , parameter 9 <35, want python take entry , output new .txt file. if either of these conditions not hold, want ignore particular line , move on. how should go writing such program?
this thing found similar problem: how convert text file list in python
however, above assumes .txt file comma-separated.
thank help!
using line.split() in order fields should work you.
for example:
def line_should_be_stored(fields): return int(fields[6]) < 30 , int(fields[8]) < 35 f_out = open('test.txt', 'wt') open('test.txt', 'rt') f_in: line in f_in: fields = line.split(); if line_should_be_stored(fields): f_out.write(line) f_out.close()
Comments
Post a Comment