I have a file in Notepad++. I want to get certain columns from the file and convert it to csv file format. Is it possible with help of some code to extract specific values from the file and save to a csv file?
Expected :
I have a file in Notepad++. I want to get certain columns from the file and convert it to csv file format. Is it possible with help of some code to extract specific values from the file and save to a csv file?
Expected :
It appears this is the PCH file format. source
Digging in, I found the following PyPi project that may simplify this for you.
https://pypi.org/project/nastran_pch_reader/
Using this I was able to take this source
$TITLE = 1
$SUBTITLE= 2
$LABEL = LC1_FX_SCHUB 3
$DISPLACEMENTS 4
$REAL OUTPUT 5
$SUBCASE ID = 1 6
87 G 1.629659E+00 -2.945256E-03 -1.814055E-01 7
-CONT- 0.000000E+00 0.000000E+00 0.000000E+00 8
88 G 1.441063E+00 -3.940322E-03 -1.789713E-01 9
-CONT- 0.000000E+00 0.000000E+00 0.000000E+00 10
89 G 1.256296E+00 -4.810100E-03 -1.743457E-01 11
And run the following python
>>> import nastran_pch_reader >>> parser = nastran_pch_reader.PchParser('mypch.pch')
>>> dir(parser)
['_PchParser__get_data_per_request', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'cur_data_chunks', 'cur_entity_id', 'cur_entity_type_id', 'cur_output', 'cur_request', 'cur_subcase', 'current_frequency', 'get_accelerations', 'get_displacements', 'get_forces', 'get_frequencies', 'get_mpcf', 'get_spcf', 'get_subcases', 'health_check', 'insert_current_frame', 'is_frequency_response', 'output_sort', 'parsed_data', 'reset_current_frame', 'validate']
>>> # Now I can check out some variables/functions!
>>>
>>> parser.parsed_data {'DISPLACEMENTS': {1: {88: [1.441063, -0.003940322, -0.1789713, 0.0, 0.0, 0.0], 89: [1.256296, -0.0048101, -0.1743457], 87: [1.629659, -0.002945256, -0.1814055, 0.0, 0.0, 0.0]}}, 'FREQUENCY': {1: {}}, 'ELEMENT STRAINS': {}, 'ELEMENT FORCES': {}, 'SPCF': {}, 'MPCF': {}, 'ACCELERATION': {}, 'SUBCASES': {1}}
>>>
>>>
>>> parser.get_displacements(1)
{88: [1.441063, -0.003940322, -0.1789713, 0.0, 0.0, 0.0], 89: [1.256296, -0.0048101, -0.1743457], 87: [1.629659, -0.002945256, -0.1814055, 0.0, 0.0, 0.0]}
>>>
>>> # this library isn't too featureful, get_displacements() only lets us get 1 at a time
>>> parser.parsed_data['DISPLACEMENTS']
{1: {88: [1.441063, -0.003940322, -0.1789713, 0.0, 0.0, 0.0], 89: [1.256296, -0.0048101, -0.1743457], 87: [1.629659, -0.002945256, -0.1814055, 0.0, 0.0, 0.0]}}
>>> for frame, values in parser.parsed_data['DISPLACEMENTS'].items(): ... for row, value in sorted(values.items()): ... print('{frame} : {row} - {value}'.format(frame=frame, row=row, value=value)) ...
1 : 87 - [1.629659, -0.002945256, -0.1814055, 0.0, 0.0, 0.0]
1 : 88 - [1.441063, -0.003940322, -0.1789713, 0.0, 0.0, 0.0]
1 : 89 - [1.256296, -0.0048101, -0.1743457]
From here you can transform this data set how you need, and write it to csv. This answer may need to be expanded upon to write your specific format. However, here is duplicate question as it relates to writing to csv.