Deleting lines in a text file
A sample of the following text file I have:
> 1 -4.6 -4.6 -7.6
>
> 2 -1.7 -3.8 -3.1
>
> 3 -1.6 -1.6 -3.1
the data is separated by tabs in the text file and the first column indicates the position.
I need to iterate over each value in a text file separately from column 0 and find the smallest value.
once the lowest value is found, the value should be written to a new text file along with the name and position of the column. Column 0 is named "position" Column 1 "fifteen", column 2 "sixteen" and column "seventeen"
for example, the lowest value in the above data is "-7.6" and is in column 3, which is labeled "seventeen". Therefore, "7.6", "seventeen" and its position value, which in this case is 1, must be written to a new text file.
Then I need to delete multiple lines from the specified text file.
eg. the lowest value is above "-7.6" and is at position "1" and is in column 3, which is called "seventeen". So I need seventeen lines to be removed from the text file, starting at position 1 and including position 1
so the column that has the lowest value indicates the number of rows to be deleted and the position it is found in indicates the starting point of the deletion
source to share
Here's the kick I think you wanted (although your requirements were pretty tough):
def extract_bio_data(input_path, output_path):
#open the output file and write it headers
output_file = open(output_path, 'w')
output_file.write('\t'.join(('position', 'min_value', 'rows_skipped')) + '\n')
#map column indexes (after popping the row number) to the number of rows to skip
col_index = { 0: 15,
1: 16,
2: 17 }
skip_to_position = 0
for line in open(input_path, 'r'):
#remove the '> ' from the beginning of the line and strip newline characters off the end
line = line[2:].strip()
#if the line contains no data, skip it
if line == '':
continue
#split the columns on whitespace (change this to split('\t') for splitting only on tabs)
columns = line.split()
#extract the row number/position of this data
position = int(columns.pop(0))
#this is where we skip rows/positions
if position < skip_to_position:
continue
#if two columns share the minimum value, this will be the first encountered in the list
min_index = columns.index(min(columns, key=float))
#this is an integer version of the 'column name' which corresponds to the number of rows that need to be skipped
rows_to_skip = col_index[min_index]
#write data to your new file (row number, minimum value, number of rows skipped)
output_file.write('\t'.join(str(x) for x in (position, columns[min_index], rows_to_skip)) + '\n')
#set the number of data rows to skip from this position
skip_to_position = position + rows_to_skip
if __name__ == '__main__':
in_path = r'c:\temp\test_input.txt'
out_path = r'c:\temp\test_output.txt'
extract_bio_data(in_path, out_path)
What was not clear to me:
- Is there really a ">" at the beginning of each line or is it a copy / paste error?
- I assumed this was not a bug.
- Do you want to write "7.6" or "-7.6" to a new file?
- I assumed you want the original value.
- Do you want to skip lines in the file? or positions based on the first column?
- I assumed you want to skip positions.
- You say you want to remove data from the original file.
- I assumed that skipping the position is enough.
source to share