Python Forum
How to remove form feed from file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to remove form feed from file
#1
Greetings for the day.
I have created a script which is used to extract a certain section of data from the file, which works properly. But in the specific data which I want to extract Form Feed and in next line [] is present which I don't want. The Form Feed which appear in the file is:- ['STAAD', 'SPACE', '--', 'PAGE', 'NO.', '']

import csv
found_type = False

filename = (r'C:\Users\GURBHEJ SINGH\Desktop\3d_frame.txt')
with open(filename, 'r') as f:
    
    for line in f:
        if line == '\n':
            continue
        
        if '0.00  MAX' in line:

            found_type = True

        if found_type:
            if '|---------------------------------------------------------------------------' in line:
               found_type = False
                    
            else:
                t_line = str(line)
                t_line = list(t_line.split())
                print(t_line)
Output:
['1', '0.00', 'MAX', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] ['MIN', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] ['0.31', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] ['MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] ['0.62', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] ['MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] ['0.92', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] ['MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] ['1.23', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] ['MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] ['1.54', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] ['MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] ['1.85', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] ['MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] ['2.15', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] ['MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] ['2.46', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] ['MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] ['2.77', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] ['MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] ['3.08', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] ['MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] ['3.38', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] ['MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] ['3.69', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] ['MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] ['4.00', 'MAX', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] ['MIN', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] ['2', '0.00', 'MAX', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] ['MIN', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] ['STAAD', 'SPACE', '--', 'PAGE', 'NO.', '6'] [] ['0.31', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] ['MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] ['0.62', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] ['MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] ['0.92', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] ['MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] ['1.23', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] ['MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] ['1.54', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] ['MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] ['1.85', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] ['MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] ['2.15', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] ['MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] ['2.46', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] ['MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] ['2.77', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] ['MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] ['3.08', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] ['MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] ['3.38', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] ['MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] ['3.69', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] ['MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] ['4.00', 'MAX', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] ['MIN', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] ['3', '0.00', 'MAX', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] ['MIN', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] ['0.31', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] ['MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] ['0.62', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] ['MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] ['0.92', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] ['MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] ['1.23', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] ['MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] ['1.54', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] ['MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] ['1.85', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] ['MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] ['2.15', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] ['MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] ['STAAD', 'SPACE', '--', 'PAGE', 'NO.', '7'] [] ['2.46', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] ['MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] ['2.77', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] ['MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] ['3.08', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] ['MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] ['3.38', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] ['MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] ['3.69', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] ['MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] ['4.00', 'MAX', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] ['MIN', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] ['4', '0.00', 'MAX', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] ['MIN', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] ['0.31', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] ['MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] ['0.62', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] ['MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] ['0.92', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] ['MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] ['1.23', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] ['MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] ['1.54', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] ['MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] ['1.85', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] ['MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] ['2.15', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] ['MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] ['2.46', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] ['MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] ['2.77', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] ['MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] ['3.08', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] ['MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] ['3.38', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] ['MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] ['3.69', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] ['MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] ['4.00', 'MAX', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] ['MIN', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] ['STAAD', 'SPACE', '--', 'PAGE', 'NO.', '8'] []
Input file :- https://drive.google.com/file/d/1aZfrjVQ...8Tmoi/view


Please suggest me the way how to remove the Form Feed and [] so that when I run the script to get specific data from the file Form Feed and [] doesn't appear in the data.
Thank You.
Reply
#2
You could have another file open as an output file. If a line either starts with "['STAAD', 'SPACE', '--', 'PAGE', 'NO.'" or is equal to '[]' you don't write it to the output file, otherwise you do.

Or you could store all of the data to a list, and after you are done reading the file, you could open it again in write mode (open(file_name, 'w')), and write the lines in the list to the file. That replaces the file rather than creating a new one, but requires the whole file to be loaded into memory.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
I think it would be good to put it into a function to make any further operations more comfortable to write
import csv

def extract_lists():
    output_lists = []
    found_type = False
    
    with open('3d_frame.txt', 'r') as f:
        
        for line in f.readlines():
            
            if '0.00  MAX' in line:
                found_type = True
                new_list = []
                
            if found_type:
                
                if '|------' in line: 
                   found_type = False
                   output_lists.append(new_list)
                   
                elif 'STAAD' not in line:
                    l = line.split()
                    
                    if l:
                        new_list.append(l)          
    return output_lists

for i, nested_list in enumerate(extract_lists(), 1):
    print('\n\nLIST NUMBER:', i)
    for data_list in nested_list:
        print(data_list)
Output:
LIST NUMBER: 1 ['1', '0.00', 'MAX', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] ['MIN', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] ['0.31', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] ['MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] ['0.62', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] ['MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] ['0.92', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] ['MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] ['1.23', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] ['MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] ['1.54', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] ['MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] ['1.85', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] ['MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] ['2.15', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] ['MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] ['2.46', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] ['MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] ['2.77', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] ['MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] ['3.08', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] ['MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] ['3.38', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] ['MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] ['3.69', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] ['MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] ['4.00', 'MAX', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] ['MIN', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] LIST NUMBER: 2 ['2', '0.00', 'MAX', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] ['MIN', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] ['0.31', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] ['MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] ['0.62', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] ['MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] ['0.92', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] ['MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] ['1.23', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] ['MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] ['1.54', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] ['MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] ['1.85', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] ['MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] ['2.15', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] ['MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] ['2.46', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] ['MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] ['2.77', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] ['MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] ['3.08', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] ['MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] ['3.38', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] ['MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] ['3.69', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] ['MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] ['4.00', 'MAX', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] ['MIN', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] LIST NUMBER: 3 ['3', '0.00', 'MAX', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] ['MIN', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] ['0.31', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] ['MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] ['0.62', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] ['MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] ['0.92', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] ['MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] ['1.23', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] ['MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] ['1.54', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] ['MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] ['1.85', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] ['MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] ['2.15', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] ['MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] ['2.46', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] ['MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] ['2.77', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] ['MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] ['3.08', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] ['MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] ['3.38', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] ['MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] ['3.69', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] ['MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] ['4.00', 'MAX', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] ['MIN', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] LIST NUMBER: 4 ['4', '0.00', 'MAX', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] ['MIN', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] ['0.31', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] ['MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] ['0.62', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] ['MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] ['0.92', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] ['MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] ['1.23', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] ['MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] ['1.54', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] ['MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] ['1.85', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] ['MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] ['2.15', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] ['MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] ['2.46', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] ['MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] ['2.77', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] ['MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] ['3.08', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] ['MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] ['3.38', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] ['MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] ['3.69', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] ['MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] ['4.00', 'MAX', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] ['MIN', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2']
Reply
#4
Thank you @michalmonday for your reply. Your code solves my problem.
I want one more help from you. I have done some additions in my previous code which I have posted earlier (first addition is for formatting the output to make it presentable and second addition is for storing the output data in CSV format). The changes are shown in the code below with its output.

import csv
found_type = False
t_ype = []

filename = (r'C:\Users\GURBHEJ SINGH\Desktop\3d_frame.txt')
with open(filename, 'r') as f:
    
    for line in f:
        if line == '\n':
            continue
        
        if '0.00  MAX' in line:
        
            found_type = True

        if found_type:
            if '|---------------------------------------------------------------------------' in line:
               found_type = False
                    
            else:
                t_line = str(line)
                t_line = list(t_line.split())

########## addtion to set the output according to csv file ##################################

                if any((item == 'MAX' or item == 'MIN') for item in t_line):
                    if len(t_line) == 9:
                        t_line.insert(0, " ")
                        t_line.insert(0, " ")
                    elif len(t_line) == 10:
                        t_line.insert(0, " ")

###############################################################################################
                        
                t_ype.append(t_line)
                print(t_line)
                
############## save to csv #######################

file_csv = 'analysis_data.csv'
with open(file_csv, 'w') as filecsv:
    write_out = csv.writer(filecsv)
    write_out.writerows(t_ype)

####################################################
Output:
['1', '0.00', 'MAX', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] [' ', '0.31', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] [' ', '0.62', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] [' ', '0.92', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] [' ', '1.23', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] [' ', '1.54', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] [' ', '1.85', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] [' ', '2.15', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] [' ', '2.46', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] [' ', '2.77', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] [' ', '3.08', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] [' ', '3.38', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] [' ', '3.69', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] [' ', '4.00', 'MAX', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] ['2', '0.00', 'MAX', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] ['STAAD', 'SPACE', '--', 'PAGE', 'NO.', '6'] [] [' ', '0.31', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] [' ', '0.62', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] [' ', '0.92', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] [' ', '1.23', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] [' ', '1.54', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] [' ', '1.85', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] [' ', '2.15', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] [' ', '2.46', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] [' ', '2.77', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] [' ', '3.08', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] [' ', '3.38', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] [' ', '3.69', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] [' ', '4.00', 'MAX', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] ['3', '0.00', 'MAX', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] [' ', '0.31', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] [' ', '0.62', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] [' ', '0.92', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] [' ', '1.23', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] [' ', '1.54', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] [' ', '1.85', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] [' ', '2.15', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] ['STAAD', 'SPACE', '--', 'PAGE', 'NO.', '7'] [] [' ', '2.46', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] [' ', '2.77', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] [' ', '3.08', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] [' ', '3.38', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] [' ', '3.69', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] [' ', '4.00', 'MAX', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] ['4', '0.00', 'MAX', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] [' ', '0.31', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] [' ', '0.62', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] [' ', '0.92', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] [' ', '1.23', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] [' ', '1.54', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] [' ', '1.85', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] [' ', '2.15', 'MAX', '4.93', '2', '57.54', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '3.29', '1', '38.36', '1', '0.00', '2', '0.00', '2'] [' ', '2.46', 'MAX', '14.79', '2', '54.51', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '9.86', '1', '36.34', '1', '0.00', '2', '0.00', '2'] [' ', '2.77', 'MAX', '24.65', '2', '48.44', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '16.43', '1', '32.29', '1', '0.00', '2', '0.00', '2'] [' ', '3.08', 'MAX', '34.51', '2', '39.34', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '23.00', '1', '26.23', '1', '0.00', '2', '0.00', '2'] [' ', '3.38', 'MAX', '44.37', '2', '27.20', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '29.58', '1', '18.14', '1', '0.00', '2', '0.00', '2'] [' ', '3.69', 'MAX', '54.23', '2', '12.04', '2', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '36.15', '1', '8.02', '1', '0.00', '2', '0.00', '2'] [' ', '4.00', 'MAX', '42.72', '1', '4.11', '1', '0.00', '2', '0.00', '2'] [' ', ' ', 'MIN', '64.08', '2', '6.17', '2', '0.00', '2', '0.00', '2'] ['STAAD', 'SPACE', '--', 'PAGE', 'NO.', '8'] []
These two additions I have done give result as required from each one of them.
But I am not able to implement these two additions in your code. Because I am not able to find where to implement these additions in the code given by you.
Can you please help me to do that.
Reply
#5
I'm not sure if your task requires using 1 csv file only but I'd use separate file for each "MEMB" list. This way you don't have to do any conversion after reading the file and you can just loop through rows and reference values by their name (e.g. row['DISTANCE'], row['max_LD']).

import csv


'''

Part to convert data into csv format

'''

def extract_lists():
    output_lists = []
    found_type = False
    
    with open('3d_frame.txt', 'r') as f:
        
        for line in f.readlines():
            
            if '0.00  MAX' in line:
                found_type = True
                new_list = [line.split()[1:]] # ommit the "MEMB"
                continue
                
                
            if found_type:
                
                if '|------' in line: 
                   found_type = False
                   output_lists.append(new_list)
                   
                elif 'STAAD' not in line:
                    l = line.split()
                    
                    if l:
                        new_list.append(l)          
    return output_lists


def convert_to_csv_files():
    value_types = 'FY,LD,MZ,LD_2,FZ,LD_3,MY,LD_4'.split(',')
    header = 'DISTANCE,' + ','.join('max_' + v for v in value_types) + ',' + ','.join('min_' + v for v in value_types)
    #header = 'DISTANCE,max_FY,max_LD,max_MZ,max_LD,max_FZ,max_LD,max_MY,max_LD,min_FY,min_LD,min_MZ,min_LD,min_FZ,min_LD,min_MY,min_LD'

    for i, nested_list in enumerate(extract_lists(), 1):
        with open(f'memb_{i}.csv', 'w') as f:
            f.write(header + '\n')
            for j in range(0, len(nested_list), 2):
                del nested_list[j][1] # remove "MAX" ("MIN" is removed using indices, "MAX" was preceded with DISTANCE)
                f.write(','.join(nested_list[j]) + ',' + ','.join(nested_list[j+1][1:]) + '\n')

convert_to_csv_files()



'''

Part to read the newly created csv files and use data

'''


def get_memb(num):
    with open(f'memb_{num}.csv', 'r') as f:
        return list( csv.DictReader(f) )

for i in range(1,5):
    print(f'MEMB {i}:')
    for row in get_memb(i):
        print(row['DISTANCE'], row['max_FY'], row['min_FY'])

    print('\n')
Output:
MEMB 1: 0.00 64.08 42.72 0.31 54.23 36.15 0.62 44.37 29.58 0.92 34.51 23.00 1.23 24.65 16.43 1.54 14.79 9.86 1.85 4.93 3.29 2.15 4.93 3.29 2.46 14.79 9.86 2.77 24.65 16.43 3.08 34.51 23.00 3.38 44.37 29.58 3.69 54.23 36.15 4.00 42.72 64.08 MEMB 2: 0.00 64.08 42.72 0.31 54.23 36.15 0.62 44.37 29.58 0.92 34.51 23.00 1.23 24.65 16.43 etc.
Having this kind of files allows you to use pandas to easily do operations on whole columns:
import pandas as pd
df = pd.read_csv('memb_1.csv')
print(df['max_FY'])
Output:
0 64.08 1 54.23 2 44.37 3 34.51 4 24.65 5 14.79 6 4.93 7 4.93 8 14.79 9 24.65 10 34.51 11 44.37 12 54.23 13 42.72 Name: max_FY, dtype: float64
print(df)
Output:
DISTANCE max_FY max_LD max_MZ ... min_FZ min_LD_3 min_MY min_LD_4 0 0.00 64.08 2 6.17 ... 0.0 2 0.0 2 1 0.31 54.23 2 12.04 ... 0.0 2 0.0 2 2 0.62 44.37 2 27.20 ... 0.0 2 0.0 2 3 0.92 34.51 2 39.34 ... 0.0 2 0.0 2 4 1.23 24.65 2 48.44 ... 0.0 2 0.0 2 5 1.54 14.79 2 54.51 ... 0.0 2 0.0 2 6 1.85 4.93 2 57.54 ... 0.0 2 0.0 2 7 2.15 4.93 2 57.54 ... 0.0 2 0.0 2 8 2.46 14.79 2 54.51 ... 0.0 2 0.0 2 9 2.77 24.65 2 48.44 ... 0.0 2 0.0 2 10 3.08 34.51 2 39.34 ... 0.0 2 0.0 2 11 3.38 44.37 2 27.20 ... 0.0 2 0.0 2 12 3.69 54.23 2 12.04 ... 0.0 2 0.0 2 13 4.00 42.72 1 4.11 ... 0.0 2 0.0 2 [14 rows x 17 columns]
Reply
#6
Thank you @michalmonday for your reply.
I want to use this CSV file for an Octave script. But in that, I want my Output arranged like this " Member, Distance, max_Fy, min_Fy, max_Mz, min_Mz " as shown in the image below, nothing else is needed. I tried to do the changes in your code to get the output as I want, but I am not able to do that.
Can you please help me solve this issue.
Output as i want is shown in image below
[Image: open?id=140ntZ8ftbD6bvuqW7RpA8eEj5TaEZyj-]

I want to ask one more question is this possible to get the output for all members in one CSV file with the code given by you. Because I also tried to do that, not able to get the output in one file.
Output in One file as shown by the file below
[Image: open?id=1jlZMqWmRe2SDSBILdvB74j4QXXEjIpfp]

Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Draw bounding boxes on live feed Jerome 0 280 Jan-20-2024, 10:50 PM
Last Post: Jerome
  How to parse a live feed in Python? Daring_T 2 4,094 Jan-20-2022, 04:17 AM
Last Post: Daring_T
  Feed List items with Integer euras 9 3,940 May-19-2021, 07:45 PM
Last Post: snippsat
  Remove single and double quotes from a csv file in 3 to 4 column shantanu97 0 6,974 Mar-31-2021, 10:52 AM
Last Post: shantanu97
  how to remove \n from file? shams 7 3,253 Feb-04-2021, 07:56 AM
Last Post: shams
  splitting UAV/sat images to smaller pieces in order to feed a CNN hobbyist 0 1,525 Dec-08-2020, 11:48 AM
Last Post: hobbyist
  Rotation Effect on live Webcam Feed Leziiy 0 1,604 Sep-12-2020, 04:25 PM
Last Post: Leziiy
  Web Form to Python Script to Text File to zip file to web wfsteadman 1 2,131 Aug-09-2020, 02:12 PM
Last Post: snippsat
  Replace every 20th space with line feed T_Lafferty 3 2,361 Jul-19-2020, 10:37 PM
Last Post: T_Lafferty
  Read microcontroller eeprom and feed into python to find checksums. mikeak2001 2 2,780 Mar-25-2020, 04:57 PM
Last Post: mikeak2001

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020