-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpandastran.py
162 lines (131 loc) · 4.78 KB
/
pandastran.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# -*- coding: utf-8; py-indent-offset:4 -*-
###############################################################################
#
# Copyright (C) Mineo Sudo
#
###############################################################################
import pandas as pd
class pandastran(object):
'''
pandas + nastran
optistruct
'''
def __init__(self):
self.out_file_path = "out_file.dat"
self.df_in=pd.DataFrame()
self.BD_ignore_list = ["+"," "]
def read_file(self,in_file_path):
#リファクタリングしましょう!
"""read input file(for all format)
Arguments:
in_file_path {path} -- read file path
"""
with open(in_file_path, 'r', encoding="utf-8") as bdf_file:
lines = bdf_file.readlines()
self.df_in["text"]=lines
self.df_in["row_no"]=self.df_in.index
self.df_in = self.df_in.replace("\n","",regex=True)
df=self.df_in.copy()
try:
separate_row = df[df["text"].str.contains("BULK")].index[0]+1
df=df[df["text"]!=""]
df=df[~df["text"].str.startswith("$")]
df=df[~df["text"].str.startswith("END")]
df["card"]=df["text"].str[:8]
df=df.reset_index(drop=True)
self.df_in_EC_CC = df[:separate_row]
self.df_in_BD = df[separate_row:]
df = self.df_in_BD.copy()
df = df["card"].drop_duplicates()
df = df[~df.str.startswith(" ")]
df = df.str.replace(",","")
df = df.str.strip()
#BD_ignore_listに含まれるものを落とす
self.df_in_BD_card = df
except:
print("Nothing BULK")
def write_file(self,df_out):
"""write out_put file
Arguments:
df_out {pandas dataframe} -- must colum "text" -< output text
"""
df_out=df_out+"\n"
with open(self.out_file_path, 'w', encoding='utf8') as bdf_file:
bdf_file.writelines(df_out["text"].tolist())
print("out file " + self.out_file_path )
def _make_df_card(self,card_name):
"""make card dataframe
Arguments:
card_name {str} -- card name ex)GRID
Returns:
pandas dataframe -- only card dataframe
"""
df=self.df_in.copy()
df=df[df["text"].str.contains(card_name)]
start_num = df.index[0]
count_row = df.index[1]-df.index[0]
end_num = df.tail(1).index[0]+count_row
df_card=self.df_in.copy()
df_card=df_card[start_num:end_num]
return df_card
def _separate_8moji(self,df):
"""hyper mesh output dat or fem separete colunm
Arguments:
df {pandas dataframe} -- one colum dataframe ex)"text"colum only
Returns:
pandas dataframe -- 8string separate dataframe
"""
moji = lambda x: x[8:]
for i in range(0,11,1):
df[str(i)]=df["text"].str.extract('(........)',expand=True)
df["text"]= df["text"].map(moji)
df = df.drop('text', axis=1)
df = df.dropna(axis=1)
return df
def _separate_open_vsp(self,df):
"""openvsp output dat separate colum
Arguments:
df {pandas dataframe} -- one colum dataframe ex)"text"colum only
Returns:
pandas dataframe -- ","separate dataframe
"""
df = df["text"].str.split(',', expand=True)
return df
def _eight_moji(self,df):
"""formatting eight string for make output file
Arguments:
df {pandas dataframe} -- any dataframe OK
Returns:
df -- string dataframe 8string
"""
moji = lambda x: x + " "* (8-len(x))
df= df.applymap(moji)
return df
def _create_output_row(self,df):
"""crate output dataframe
Arguments:
df {pandas dataframe} -- any dataframe OK
Returns:
pandas dataframe -- for out put
"""
df=df.assign(
text=lambda df: df.apply(lambda row: "".join(row), axis=1) # A-Eの和
)
return df[["text"]]
def make_dataframe_from_lists(self,li):
"""',' separate list to pandas dataframe
Arguments:
li {list} -- list "," separate some lists
Returns:
pandas dataframe -- you can use output
"""
df_out = pd.DataFrame()
for i in li:
try:
df_temp = pd.DataFrame(i)
df_out = pd.concat([df_out,df_temp])
except:
df_out = pd.DataFrame(i)
df_out = df_out.reset_index(drop=True)
df_out.columns = ["text"]
return df_out