Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help parsing file
#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


Messages In This Thread
help parsing file - by aslezak - Oct-18-2019, 06:04 PM
RE: help parsing file - by buran - Oct-18-2019, 06:11 PM
RE: help parsing file - by aslezak - Oct-22-2019, 03:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,981 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,730 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Parsing xml file deletes whitespaces. How to avoid it? Paqqno 0 1,059 Apr-01-2022, 10:20 PM
Last Post: Paqqno
  Parsing a syslog file ebolisa 11 4,199 Oct-10-2021, 05:15 PM
Last Post: snippsat
Thumbs Up Parsing a YAML file without changing the string content..?, Flask - solved. SpongeB0B 2 2,305 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B
  File Name Parsing millpond 5 3,669 Aug-26-2020, 08:04 AM
Last Post: bowlofred
  Error while parsing tables from docx file aditi 1 3,764 Jul-14-2020, 09:24 PM
Last Post: aditi
  Python Script for parsing dictionary values from yaml file pawan6782 3 4,975 Sep-04-2019, 07:21 PM
Last Post: pawan6782
  Parsing an MBOX file Oliver 1 8,219 May-26-2019, 07:12 AM
Last Post: heiner55
  parsing complex text file anna 1 2,094 Apr-10-2019, 09:54 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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