Python Forum
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
Reply
#2
please, use output tags to show the correct format of the file
See BBcode help for more info.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#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() 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading an ASCII text file and parsing data... oradba4u 2 2,802 Jun-08-2024, 12:41 AM
Last Post: oradba4u
Video doing data treatment on a file import-parsing a variable EmBeck87 15 7,779 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Modify values in XML file by data from text file (without parsing) Paqqno 2 3,969 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Parsing xml file deletes whitespaces. How to avoid it? Paqqno 0 2,186 Apr-01-2022, 10:20 PM
Last Post: Paqqno
  Parsing a syslog file ebolisa 11 8,303 Oct-10-2021, 05:15 PM
Last Post: snippsat
Thumbs Up Parsing a YAML file without changing the string content..?, Flask - solved. SpongeB0B 2 3,830 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B
  File Name Parsing millpond 5 8,296 Aug-26-2020, 08:04 AM
Last Post: bowlofred
  Error while parsing tables from docx file aditi 1 7,078 Jul-14-2020, 09:24 PM
Last Post: aditi
  Python Script for parsing dictionary values from yaml file pawan6782 3 7,590 Sep-04-2019, 07:21 PM
Last Post: pawan6782
  Parsing an MBOX file Oliver 1 11,958 May-26-2019, 07:12 AM
Last Post: heiner55

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.