Creating lists from a non-csv .txt file; output to new .txt based on variable conditions

148 views Asked by At

I have two quick python questions. This is my first experience with python; I have almost no programming experience whatsoever so I apologize - this is a very basic question and I know it should be easy. I did search for existing answers but could not find anything that helped.

Basically, I have a .txt file that keeps up with 31 variables. There is a header at the top with category names, and then the following lines are just the entries. It looks like this:

date time parameter3 ... parameter31
 d1   t1   p3_1      ... p31_1
 d2   t2   p3_2      ... p31_2 

etc.

All of the parameters are numerical, except for parameter6, which will be either a string of three letters or if it is empty will be "..."

The .txt file is not comma-separated; only one to three whitespaces separate each value.

I am trying to import this text file into Python, create a list out of each line, and then have Python check parameter7 and parameter9. If parameters 7 is <30 and parameter 9 is <35, I want Python to take that entry and output it to a new .txt file. If either of these conditions does not hold, I want it to ignore that particular line and move on. How should I go about writing such a program?

This is the only thing I found that was similar to my problem: How to Convert a Text File into a List in Python

However, the above assumes the .txt file is already comma-separated.

Thank you for your help!

2

There are 2 answers

8
Roberto On

Using line.split() in order to get the fields should work for you.

for example:

def line_should_be_stored(fields):
    return int(fields[6]) < 30 and int(fields[8]) < 35

f_out = open('test.txt', 'wt') 
with open('test.txt', 'rt') as f_in:
    for line in f_in:
        fields = line.split();
        if line_should_be_stored(fields):
            f_out.write(line)
f_out.close()
0
PythonTester On

create a write method:

def write_method(row)
    """
    Note this method will append to an exsisting file, 
    if you do not want that then remove conditional statement below and replace 
    'a' with 'w+' in the with statement.
    """
    if not os.path.isfile('somefile.txt'):
        with open('somefile.txt' 'w+')

    with open('somefile.txt', 'a') as the_file:
          the_file.write(row)

The link you supply is the right answer, you just need to replace :

for i in line.split(","):

with

for i in line.split("   "):

This will give you:

["d1", "t1", "p3_1", "...", "p31_1", "d2", "t2", "p3_2", "...", "p31_2"]

for each line instead of append you can grab "line[6] and line[8]"

if line[6] is <30 and line[8] is <35:
    row.append(line)

If you require the lines on a new line each time 'row.append(line)' becomes

row.append('%s\n' % line)

after parsing the file you can then pass row to the file write method

 write_method(row)