Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 help parsing file
#1
I'm looking for general guidance/suggestions on how to extract grid data points from a text file. Lines beginning with "GRID*" have node numbers and x y z coordinate information. I would like to extract that information. Each "GRID" line is segmented into '6' columns. The first and last column are eight characters wide, columns 2,3,4, and 5 are 16 characters wide. An example coordinate would be '-3.69374569427+0'. I would also like to know how to convert that format into a real number. I studied the "strings" documentation but haven't found an answer on my own yet. Any help/suggestions/guidance appreciated.

Thanks!,
Andrew

sample file contents:

Output:
GRID* 1 0-3.69374569427+09.077207667771+0 * 5.000014684254-1 0 GRID* 2 0-3.69374569427+0-9.07720766777+0 * 4.999985315745-1 0 GRID* 3 08.978071514315+03.928573996764+0 * 4.999963281080-1 0 GRID* 4 08.978071514315+0-3.92857399676+0 * 5.000036718919-1 0
etc..

PS.
I tried to show the "column widths" in the sample file contents but the forum post removes the white space. The coordinate information begins in column 4 and continues on the next line. For anyone curious, this is from a NASTRAN input file
Quote
#2
please, use output tags to show the correct format of the file
See BBcode help for more info.
Quote
#3
This is what I have so far. I'm not sure how robust it is

import csv
import tkinter as tk
from tkinter import filedialog
from math import *

root = tk.Tk()
root.withdraw()

#opened file must be NASTRAN input file. Check .nas extension
file_path = filedialog.askopenfilename()

nas = open(file_path ,mode = 'r', encoding = 'utf-8')

n=[]
x=[]
y=[]
z=[]
nextline = 0
for line in nas:
    if line[:4] == "GRID":
        nextline = 1
        n.append(line[8:8 + 16])
        x.append(line[8 + 2*16:8+3*16])
        y.append(line[8 + 3*16:8+4*16])
        continue    
    if nextline == 1:
        z.append(line[8:8+16])
        nextline = 0

def scientific(coord):
    ind = 0
    for i in coord:
        if i.find('+') > -1:
            coord[ind] = i.replace('+', 'E+')
        elif i.find('-') > -1:
            if i.find('-') == 0:
                s = i.lstrip('-')
                i = '-' + s.replace('-', 'E-')
            else:
                i = i.replace('-', 'E-')
            coord[ind] = i
        ind = ind + 1    

scientific(x)
scientific(y)
scientific(z)
        
with open('gridout.csv', 'w', newline='') as csvFile:
    writer = csv.writer(csvFile)
    index = 0
    for i in n:
        writer.writerow([n[index], x[index], y[index], z[index]])
        index = index + 1

nas.close() 
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Python Script for parsing dictionary values from yaml file pawan6782 3 1,092 Sep-04-2019, 07:21 PM
Last Post: pawan6782
  Parsing an MBOX file Oliver 1 2,332 May-26-2019, 07:12 AM
Last Post: heiner55
  parsing complex text file anna 1 589 Apr-10-2019, 09:54 PM
Last Post: Larz60+
  Parsing file and get a specific text dds69 4 889 Nov-12-2018, 08:06 AM
Last Post: dds69
  Parsing Text file having repeated value key pair using python manussnair 3 834 Aug-04-2018, 11:48 PM
Last Post: micseydel
  Parsing and Editing a Structured Text File norsemanGrey 1 780 Jul-11-2018, 09:51 PM
Last Post: Larz60+
  Python file parsing, can't catch strings in new line Philia 5 1,248 Apr-29-2018, 01:09 PM
Last Post: snippsat
  unexpected output while parsing file anna 3 1,047 Apr-28-2018, 05:13 PM
Last Post: anna
  multi-threaded file parsing bb8 3 1,037 Apr-26-2018, 05:12 PM
Last Post: woooee
  Parsing Text File standenman 0 910 Jan-24-2018, 11:00 PM
Last Post: standenman

Forum Jump:


Users browsing this thread: 1 Guest(s)