How do I parse multiple files in Python and extract important information?How do I check whether a file exists without exceptions?How do I copy a file in Python?How can I safely create a nested directory in Python?How do I parse a string to a float or int in Python?How to get the current time in PythonHow can I make a time delay in Python?Why can't Python parse this JSON data?How do I list all files of a directory?How do you parse and process HTML/XML in PHP?“Large data” work flows using pandas
How can the Zone of Truth spell be defeated without the caster knowing?
What do the phrase "Reeyan's seacrest" and the word "fraggle" mean in a sketch?
What is the strongest case that can be made in favour of the UK regaining some control over fishing policy after Brexit?
A Note on N!
The Defining Moment
What was the first Intel x86 processor with "Base + Index * Scale + Displacement" addressing mode?
How can Republicans who favour free markets, consistently express anger when they don't like the outcome of that choice?
What is the difference between `command a[bc]d` and `command `ab,cd`
How to get a plain text file version of a CP/M .BAS (M-BASIC) program?
How could Tony Stark make this in Endgame?
Binary Numbers Magic Trick
Mac Pro install disk keep ejecting itself please help
Can someone publish a story that happened to you?
What happened to Captain America in Endgame?
Unexpected email from Yorkshire Bank
Pulling the rope with one hand is as heavy as with two hands?
what is the sudo password for a --disabled-password user
In order to check if a field is required or not, is the result of isNillable method sufficient?
Apply MapThread to all but one variable
A Strange Latex Symbol
What language was spoken in East Asia before Proto-Turkic?
Is there a way to get a compiler for the original B programming language?
Does holding a wand and speaking its command word count as V/S/M spell components?
Realistic Necromancy?
How do I parse multiple files in Python and extract important information?
How do I check whether a file exists without exceptions?How do I copy a file in Python?How can I safely create a nested directory in Python?How do I parse a string to a float or int in Python?How to get the current time in PythonHow can I make a time delay in Python?Why can't Python parse this JSON data?How do I list all files of a directory?How do you parse and process HTML/XML in PHP?“Large data” work flows using pandas
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am trying to write a program that reads data from hundreds of YAML files repetitively and stores certain information contained in the files in a table of some sort. The program would essentially parse every YAML file in a given directory and extract the relevant information until every file has been successfully parsed.
An example of the contents of one of the YAML files:
%YAML:1.0
camera_rotation_wrt_base: !!opencv-matrix
cols: 3
data: [-0.0159428846, 0.0246045925, 0.999570131, -0.999774337, -0.0144301597, -0.0155909406,
0.0140403481, -0.999593139, 0.024829099]
dt: f
rows: 3
camera_translation_wrt_base: [0.4445618987083435, 0.11700689047574997, 1.5018157958984375]
object_rotation_wrt_base: !!opencv-matrix
cols: 3
data: [-0.74130547, -0.0615471229, 0.668339849, 0.669196069, -0.144052029, 0.728989482,
0.0514085107, 0.987654269, 0.147973642]
dt: f
rows: 3
object_rotation_wrt_camera: !!opencv-matrix
cols: 3
data: [-0.6565323818253673, 0.1588616842697038, -0.737379262582055, -0.07928983462501557,
-0.9866892288165471, -0.14197683014545748, -0.7501189077886223, -0.03474571781315458,
0.6603895951165363]
dt: f
rows: 3
object_translation_wrt_base: [1.1534364223480225, 0.05951927974820137, 1.3502429723739624]
object_translation_wrt_camera: [0.04407151401699165, 0.16979082390232392, 0.705698973194305]
template_id: 1965
I would like to be able to store the data key from the object_rotation_wrt_camera key, as well as the object_translation_wrt_camera key, in a CSV file, like so:
observation,rotation,translation
1,[-0.53434, 0.023343, .....],[0.54545,0.34344,....]
2,[-0.52234, 0.3433, .....],[0.65645,0.8787344,....]
3,[0.32234, 0.6453, .....],[0.622645,0.1787344,....]
In the above table, the observation number pertains to the yaml file, and therefore for each file there is an observation stored in the CSV file for both the rotation and translation variable. (note: the periods used in the table just indicate that the rotation and translation variables continue as they are quite long).
Lastly, I would like to create a final CSV file, that is similar to the one above however instead has all of the rotation and translation values separated (meaning that instead of one column for translation and one for rotation, there are 3 for translation pertaining to the 3 values within the lists of the previous CSV file, and 9 columns pertaining to each of the 9 values within the lists of the previous CSV file), like so:
observation,tran1,tran2,tran3,rot1,rot2,rot3,rot4,rot5,rot6,rot7,rot8,rot9
1,-0.545434,4.54545,0.343434,.............................................
2,-0.4543,3.3434,0.3534,..................................................
python parsing yaml
add a comment |
I am trying to write a program that reads data from hundreds of YAML files repetitively and stores certain information contained in the files in a table of some sort. The program would essentially parse every YAML file in a given directory and extract the relevant information until every file has been successfully parsed.
An example of the contents of one of the YAML files:
%YAML:1.0
camera_rotation_wrt_base: !!opencv-matrix
cols: 3
data: [-0.0159428846, 0.0246045925, 0.999570131, -0.999774337, -0.0144301597, -0.0155909406,
0.0140403481, -0.999593139, 0.024829099]
dt: f
rows: 3
camera_translation_wrt_base: [0.4445618987083435, 0.11700689047574997, 1.5018157958984375]
object_rotation_wrt_base: !!opencv-matrix
cols: 3
data: [-0.74130547, -0.0615471229, 0.668339849, 0.669196069, -0.144052029, 0.728989482,
0.0514085107, 0.987654269, 0.147973642]
dt: f
rows: 3
object_rotation_wrt_camera: !!opencv-matrix
cols: 3
data: [-0.6565323818253673, 0.1588616842697038, -0.737379262582055, -0.07928983462501557,
-0.9866892288165471, -0.14197683014545748, -0.7501189077886223, -0.03474571781315458,
0.6603895951165363]
dt: f
rows: 3
object_translation_wrt_base: [1.1534364223480225, 0.05951927974820137, 1.3502429723739624]
object_translation_wrt_camera: [0.04407151401699165, 0.16979082390232392, 0.705698973194305]
template_id: 1965
I would like to be able to store the data key from the object_rotation_wrt_camera key, as well as the object_translation_wrt_camera key, in a CSV file, like so:
observation,rotation,translation
1,[-0.53434, 0.023343, .....],[0.54545,0.34344,....]
2,[-0.52234, 0.3433, .....],[0.65645,0.8787344,....]
3,[0.32234, 0.6453, .....],[0.622645,0.1787344,....]
In the above table, the observation number pertains to the yaml file, and therefore for each file there is an observation stored in the CSV file for both the rotation and translation variable. (note: the periods used in the table just indicate that the rotation and translation variables continue as they are quite long).
Lastly, I would like to create a final CSV file, that is similar to the one above however instead has all of the rotation and translation values separated (meaning that instead of one column for translation and one for rotation, there are 3 for translation pertaining to the 3 values within the lists of the previous CSV file, and 9 columns pertaining to each of the 9 values within the lists of the previous CSV file), like so:
observation,tran1,tran2,tran3,rot1,rot2,rot3,rot4,rot5,rot6,rot7,rot8,rot9
1,-0.545434,4.54545,0.343434,.............................................
2,-0.4543,3.3434,0.3534,..................................................
python parsing yaml
add a comment |
I am trying to write a program that reads data from hundreds of YAML files repetitively and stores certain information contained in the files in a table of some sort. The program would essentially parse every YAML file in a given directory and extract the relevant information until every file has been successfully parsed.
An example of the contents of one of the YAML files:
%YAML:1.0
camera_rotation_wrt_base: !!opencv-matrix
cols: 3
data: [-0.0159428846, 0.0246045925, 0.999570131, -0.999774337, -0.0144301597, -0.0155909406,
0.0140403481, -0.999593139, 0.024829099]
dt: f
rows: 3
camera_translation_wrt_base: [0.4445618987083435, 0.11700689047574997, 1.5018157958984375]
object_rotation_wrt_base: !!opencv-matrix
cols: 3
data: [-0.74130547, -0.0615471229, 0.668339849, 0.669196069, -0.144052029, 0.728989482,
0.0514085107, 0.987654269, 0.147973642]
dt: f
rows: 3
object_rotation_wrt_camera: !!opencv-matrix
cols: 3
data: [-0.6565323818253673, 0.1588616842697038, -0.737379262582055, -0.07928983462501557,
-0.9866892288165471, -0.14197683014545748, -0.7501189077886223, -0.03474571781315458,
0.6603895951165363]
dt: f
rows: 3
object_translation_wrt_base: [1.1534364223480225, 0.05951927974820137, 1.3502429723739624]
object_translation_wrt_camera: [0.04407151401699165, 0.16979082390232392, 0.705698973194305]
template_id: 1965
I would like to be able to store the data key from the object_rotation_wrt_camera key, as well as the object_translation_wrt_camera key, in a CSV file, like so:
observation,rotation,translation
1,[-0.53434, 0.023343, .....],[0.54545,0.34344,....]
2,[-0.52234, 0.3433, .....],[0.65645,0.8787344,....]
3,[0.32234, 0.6453, .....],[0.622645,0.1787344,....]
In the above table, the observation number pertains to the yaml file, and therefore for each file there is an observation stored in the CSV file for both the rotation and translation variable. (note: the periods used in the table just indicate that the rotation and translation variables continue as they are quite long).
Lastly, I would like to create a final CSV file, that is similar to the one above however instead has all of the rotation and translation values separated (meaning that instead of one column for translation and one for rotation, there are 3 for translation pertaining to the 3 values within the lists of the previous CSV file, and 9 columns pertaining to each of the 9 values within the lists of the previous CSV file), like so:
observation,tran1,tran2,tran3,rot1,rot2,rot3,rot4,rot5,rot6,rot7,rot8,rot9
1,-0.545434,4.54545,0.343434,.............................................
2,-0.4543,3.3434,0.3534,..................................................
python parsing yaml
I am trying to write a program that reads data from hundreds of YAML files repetitively and stores certain information contained in the files in a table of some sort. The program would essentially parse every YAML file in a given directory and extract the relevant information until every file has been successfully parsed.
An example of the contents of one of the YAML files:
%YAML:1.0
camera_rotation_wrt_base: !!opencv-matrix
cols: 3
data: [-0.0159428846, 0.0246045925, 0.999570131, -0.999774337, -0.0144301597, -0.0155909406,
0.0140403481, -0.999593139, 0.024829099]
dt: f
rows: 3
camera_translation_wrt_base: [0.4445618987083435, 0.11700689047574997, 1.5018157958984375]
object_rotation_wrt_base: !!opencv-matrix
cols: 3
data: [-0.74130547, -0.0615471229, 0.668339849, 0.669196069, -0.144052029, 0.728989482,
0.0514085107, 0.987654269, 0.147973642]
dt: f
rows: 3
object_rotation_wrt_camera: !!opencv-matrix
cols: 3
data: [-0.6565323818253673, 0.1588616842697038, -0.737379262582055, -0.07928983462501557,
-0.9866892288165471, -0.14197683014545748, -0.7501189077886223, -0.03474571781315458,
0.6603895951165363]
dt: f
rows: 3
object_translation_wrt_base: [1.1534364223480225, 0.05951927974820137, 1.3502429723739624]
object_translation_wrt_camera: [0.04407151401699165, 0.16979082390232392, 0.705698973194305]
template_id: 1965
I would like to be able to store the data key from the object_rotation_wrt_camera key, as well as the object_translation_wrt_camera key, in a CSV file, like so:
observation,rotation,translation
1,[-0.53434, 0.023343, .....],[0.54545,0.34344,....]
2,[-0.52234, 0.3433, .....],[0.65645,0.8787344,....]
3,[0.32234, 0.6453, .....],[0.622645,0.1787344,....]
In the above table, the observation number pertains to the yaml file, and therefore for each file there is an observation stored in the CSV file for both the rotation and translation variable. (note: the periods used in the table just indicate that the rotation and translation variables continue as they are quite long).
Lastly, I would like to create a final CSV file, that is similar to the one above however instead has all of the rotation and translation values separated (meaning that instead of one column for translation and one for rotation, there are 3 for translation pertaining to the 3 values within the lists of the previous CSV file, and 9 columns pertaining to each of the 9 values within the lists of the previous CSV file), like so:
observation,tran1,tran2,tran3,rot1,rot2,rot3,rot4,rot5,rot6,rot7,rot8,rot9
1,-0.545434,4.54545,0.343434,.............................................
2,-0.4543,3.3434,0.3534,..................................................
python parsing yaml
python parsing yaml
edited Mar 22 at 18:56
wallZ
asked Mar 22 at 14:52
wallZwallZ
267
267
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I stored your example in two differently files with .yaml
endings and then ran:
import sys
from pathlib import Path
import csv
import ruamel.yaml
result = [['observation', 'rotation', 'translation']]
flatres = ["observation,tran1,tran2,tran3,rot1,rot2,rot3,rot4,rot5,rot6,rot7,rot8,rot9".split(',')]
yaml = ruamel.yaml.YAML()
for idx, file_name in enumerate(Path('.').glob('*.yaml')):
txt = file_name.read_text()
if txt.startswith('%YAML:1.0'):
txt = txt.replace('%YAML:1.0', "", 1).lstrip()
data = yaml.load(txt)
result.append([
idx+1,
data['object_rotation_wrt_camera']['data'],
data['object_translation_wrt_camera'],
])
row = [idx+1]
row.extend(data['object_translation_wrt_camera'])
row.extend(data['object_rotation_wrt_camera']['data'])
flatres.append(row)
writer = csv.writer(sys.stdout)
writer.writerows(result)
print('---------')
writer = csv.writer(sys.stdout)
writer.writerows(flatres)
which gives:
observation,rotation,translation
1,"[-0.6565323818253673, 0.1588616842697038, -0.737379262582055, -0.07928983462501557, -0.9866892288165471, -0.14197683014545748, -0.7501189077886223, -0.03474571781315458, 0.6603895951165363]","[0.04407151401699165, 0.16979082390232392, 0.705698973194305]"
2,"[-0.6565323818253673, 0.1588616842697038, -0.737379262582055, -0.07928983462501557, -0.9866892288165471, -0.14197683014545748, -0.7501189077886223, -0.03474571781315458, 0.6603895951165363]","[0.04407151401699165, 0.16979082390232392, 0.705698973194305]"
---------
observation,tran1,tran2,tran3,rot1,rot2,rot3,rot4,rot5,rot6,rot7,rot8,rot9
1,0.04407151401699165,0.16979082390232392,0.705698973194305,-0.6565323818253673,0.1588616842697038,-0.737379262582055,-0.07928983462501557,-0.9866892288165471,-0.14197683014545748,-0.7501189077886223,-0.03474571781315458,0.6603895951165363
2,0.04407151401699165,0.16979082390232392,0.705698973194305,-0.6565323818253673,0.1588616842697038,-0.737379262582055,-0.07928983462501557,-0.9866892288165471,-0.14197683014545748,-0.7501189077886223,-0.03474571781315458,0.6603895951165363
Because there are commas in the sequences that you want to have
stored, those entries need to be quoted and Python's CSV writer does
that automatically (otherwise the second and following rows of the CSV
would have many more than three elements).
YAML 1.0 was superseded, by YAML 1.1, in 2005 and I have not paid
special attention to the differences between those versions apart from
the directive. YAML 1.2 has been the YAML standard since 2009.ruamel.yaml
only supports files that have explicit YAML 1.2 (or 1.1)
directives, that is why the %YAML:1.0
directive needs to be
explicitly removed from these files.
You might run into trouble if you have any "old-style" octal integers
in your files and in some other cases not shown in your input example.
This is extremely helpful, thanks so much! Only problem is when I try to specify ['data'] as per your recommendation, I get this error: TypeError: cannot convert dictionary update sequence element #0 to a sequence
– wallZ
Mar 22 at 15:58
If you tried to do the altenternative I proposed, there was an error there, i updated that.
– Anthon
Mar 22 at 16:05
Okay my apologies for being vague. What I would like to do precisely is to store the rotation and translation arrays in one table (or matrix) whereby there are 2 columns, one for rotation and one for translation. Therefore each yaml file would be considered an 'observation', for example, that involves the two variables of rotation and translation.
– wallZ
Mar 22 at 16:16
The translation is alist
, the rotation adict
(and the value fordata
in that dict is again a list. There are no arrays, table, or matrices and I have no idea what you see as difference between these three (nor if any of these three is the same as a Python list). When working with Python you should follow its terminology. Alternatively show YAML/JSON/CSV output of your expected data structure (or how it would look in python) by updating your question (and leave out "EDIT:" or any of that stuff when you do)
– Anthon
Mar 22 at 16:35
Thanks very much for your guidance/advice. I have updated the question and made it much more specific as to my goal I believe.
– wallZ
Mar 22 at 16:54
|
show 4 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55302302%2fhow-do-i-parse-multiple-files-in-python-and-extract-important-information%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I stored your example in two differently files with .yaml
endings and then ran:
import sys
from pathlib import Path
import csv
import ruamel.yaml
result = [['observation', 'rotation', 'translation']]
flatres = ["observation,tran1,tran2,tran3,rot1,rot2,rot3,rot4,rot5,rot6,rot7,rot8,rot9".split(',')]
yaml = ruamel.yaml.YAML()
for idx, file_name in enumerate(Path('.').glob('*.yaml')):
txt = file_name.read_text()
if txt.startswith('%YAML:1.0'):
txt = txt.replace('%YAML:1.0', "", 1).lstrip()
data = yaml.load(txt)
result.append([
idx+1,
data['object_rotation_wrt_camera']['data'],
data['object_translation_wrt_camera'],
])
row = [idx+1]
row.extend(data['object_translation_wrt_camera'])
row.extend(data['object_rotation_wrt_camera']['data'])
flatres.append(row)
writer = csv.writer(sys.stdout)
writer.writerows(result)
print('---------')
writer = csv.writer(sys.stdout)
writer.writerows(flatres)
which gives:
observation,rotation,translation
1,"[-0.6565323818253673, 0.1588616842697038, -0.737379262582055, -0.07928983462501557, -0.9866892288165471, -0.14197683014545748, -0.7501189077886223, -0.03474571781315458, 0.6603895951165363]","[0.04407151401699165, 0.16979082390232392, 0.705698973194305]"
2,"[-0.6565323818253673, 0.1588616842697038, -0.737379262582055, -0.07928983462501557, -0.9866892288165471, -0.14197683014545748, -0.7501189077886223, -0.03474571781315458, 0.6603895951165363]","[0.04407151401699165, 0.16979082390232392, 0.705698973194305]"
---------
observation,tran1,tran2,tran3,rot1,rot2,rot3,rot4,rot5,rot6,rot7,rot8,rot9
1,0.04407151401699165,0.16979082390232392,0.705698973194305,-0.6565323818253673,0.1588616842697038,-0.737379262582055,-0.07928983462501557,-0.9866892288165471,-0.14197683014545748,-0.7501189077886223,-0.03474571781315458,0.6603895951165363
2,0.04407151401699165,0.16979082390232392,0.705698973194305,-0.6565323818253673,0.1588616842697038,-0.737379262582055,-0.07928983462501557,-0.9866892288165471,-0.14197683014545748,-0.7501189077886223,-0.03474571781315458,0.6603895951165363
Because there are commas in the sequences that you want to have
stored, those entries need to be quoted and Python's CSV writer does
that automatically (otherwise the second and following rows of the CSV
would have many more than three elements).
YAML 1.0 was superseded, by YAML 1.1, in 2005 and I have not paid
special attention to the differences between those versions apart from
the directive. YAML 1.2 has been the YAML standard since 2009.ruamel.yaml
only supports files that have explicit YAML 1.2 (or 1.1)
directives, that is why the %YAML:1.0
directive needs to be
explicitly removed from these files.
You might run into trouble if you have any "old-style" octal integers
in your files and in some other cases not shown in your input example.
This is extremely helpful, thanks so much! Only problem is when I try to specify ['data'] as per your recommendation, I get this error: TypeError: cannot convert dictionary update sequence element #0 to a sequence
– wallZ
Mar 22 at 15:58
If you tried to do the altenternative I proposed, there was an error there, i updated that.
– Anthon
Mar 22 at 16:05
Okay my apologies for being vague. What I would like to do precisely is to store the rotation and translation arrays in one table (or matrix) whereby there are 2 columns, one for rotation and one for translation. Therefore each yaml file would be considered an 'observation', for example, that involves the two variables of rotation and translation.
– wallZ
Mar 22 at 16:16
The translation is alist
, the rotation adict
(and the value fordata
in that dict is again a list. There are no arrays, table, or matrices and I have no idea what you see as difference between these three (nor if any of these three is the same as a Python list). When working with Python you should follow its terminology. Alternatively show YAML/JSON/CSV output of your expected data structure (or how it would look in python) by updating your question (and leave out "EDIT:" or any of that stuff when you do)
– Anthon
Mar 22 at 16:35
Thanks very much for your guidance/advice. I have updated the question and made it much more specific as to my goal I believe.
– wallZ
Mar 22 at 16:54
|
show 4 more comments
I stored your example in two differently files with .yaml
endings and then ran:
import sys
from pathlib import Path
import csv
import ruamel.yaml
result = [['observation', 'rotation', 'translation']]
flatres = ["observation,tran1,tran2,tran3,rot1,rot2,rot3,rot4,rot5,rot6,rot7,rot8,rot9".split(',')]
yaml = ruamel.yaml.YAML()
for idx, file_name in enumerate(Path('.').glob('*.yaml')):
txt = file_name.read_text()
if txt.startswith('%YAML:1.0'):
txt = txt.replace('%YAML:1.0', "", 1).lstrip()
data = yaml.load(txt)
result.append([
idx+1,
data['object_rotation_wrt_camera']['data'],
data['object_translation_wrt_camera'],
])
row = [idx+1]
row.extend(data['object_translation_wrt_camera'])
row.extend(data['object_rotation_wrt_camera']['data'])
flatres.append(row)
writer = csv.writer(sys.stdout)
writer.writerows(result)
print('---------')
writer = csv.writer(sys.stdout)
writer.writerows(flatres)
which gives:
observation,rotation,translation
1,"[-0.6565323818253673, 0.1588616842697038, -0.737379262582055, -0.07928983462501557, -0.9866892288165471, -0.14197683014545748, -0.7501189077886223, -0.03474571781315458, 0.6603895951165363]","[0.04407151401699165, 0.16979082390232392, 0.705698973194305]"
2,"[-0.6565323818253673, 0.1588616842697038, -0.737379262582055, -0.07928983462501557, -0.9866892288165471, -0.14197683014545748, -0.7501189077886223, -0.03474571781315458, 0.6603895951165363]","[0.04407151401699165, 0.16979082390232392, 0.705698973194305]"
---------
observation,tran1,tran2,tran3,rot1,rot2,rot3,rot4,rot5,rot6,rot7,rot8,rot9
1,0.04407151401699165,0.16979082390232392,0.705698973194305,-0.6565323818253673,0.1588616842697038,-0.737379262582055,-0.07928983462501557,-0.9866892288165471,-0.14197683014545748,-0.7501189077886223,-0.03474571781315458,0.6603895951165363
2,0.04407151401699165,0.16979082390232392,0.705698973194305,-0.6565323818253673,0.1588616842697038,-0.737379262582055,-0.07928983462501557,-0.9866892288165471,-0.14197683014545748,-0.7501189077886223,-0.03474571781315458,0.6603895951165363
Because there are commas in the sequences that you want to have
stored, those entries need to be quoted and Python's CSV writer does
that automatically (otherwise the second and following rows of the CSV
would have many more than three elements).
YAML 1.0 was superseded, by YAML 1.1, in 2005 and I have not paid
special attention to the differences between those versions apart from
the directive. YAML 1.2 has been the YAML standard since 2009.ruamel.yaml
only supports files that have explicit YAML 1.2 (or 1.1)
directives, that is why the %YAML:1.0
directive needs to be
explicitly removed from these files.
You might run into trouble if you have any "old-style" octal integers
in your files and in some other cases not shown in your input example.
This is extremely helpful, thanks so much! Only problem is when I try to specify ['data'] as per your recommendation, I get this error: TypeError: cannot convert dictionary update sequence element #0 to a sequence
– wallZ
Mar 22 at 15:58
If you tried to do the altenternative I proposed, there was an error there, i updated that.
– Anthon
Mar 22 at 16:05
Okay my apologies for being vague. What I would like to do precisely is to store the rotation and translation arrays in one table (or matrix) whereby there are 2 columns, one for rotation and one for translation. Therefore each yaml file would be considered an 'observation', for example, that involves the two variables of rotation and translation.
– wallZ
Mar 22 at 16:16
The translation is alist
, the rotation adict
(and the value fordata
in that dict is again a list. There are no arrays, table, or matrices and I have no idea what you see as difference between these three (nor if any of these three is the same as a Python list). When working with Python you should follow its terminology. Alternatively show YAML/JSON/CSV output of your expected data structure (or how it would look in python) by updating your question (and leave out "EDIT:" or any of that stuff when you do)
– Anthon
Mar 22 at 16:35
Thanks very much for your guidance/advice. I have updated the question and made it much more specific as to my goal I believe.
– wallZ
Mar 22 at 16:54
|
show 4 more comments
I stored your example in two differently files with .yaml
endings and then ran:
import sys
from pathlib import Path
import csv
import ruamel.yaml
result = [['observation', 'rotation', 'translation']]
flatres = ["observation,tran1,tran2,tran3,rot1,rot2,rot3,rot4,rot5,rot6,rot7,rot8,rot9".split(',')]
yaml = ruamel.yaml.YAML()
for idx, file_name in enumerate(Path('.').glob('*.yaml')):
txt = file_name.read_text()
if txt.startswith('%YAML:1.0'):
txt = txt.replace('%YAML:1.0', "", 1).lstrip()
data = yaml.load(txt)
result.append([
idx+1,
data['object_rotation_wrt_camera']['data'],
data['object_translation_wrt_camera'],
])
row = [idx+1]
row.extend(data['object_translation_wrt_camera'])
row.extend(data['object_rotation_wrt_camera']['data'])
flatres.append(row)
writer = csv.writer(sys.stdout)
writer.writerows(result)
print('---------')
writer = csv.writer(sys.stdout)
writer.writerows(flatres)
which gives:
observation,rotation,translation
1,"[-0.6565323818253673, 0.1588616842697038, -0.737379262582055, -0.07928983462501557, -0.9866892288165471, -0.14197683014545748, -0.7501189077886223, -0.03474571781315458, 0.6603895951165363]","[0.04407151401699165, 0.16979082390232392, 0.705698973194305]"
2,"[-0.6565323818253673, 0.1588616842697038, -0.737379262582055, -0.07928983462501557, -0.9866892288165471, -0.14197683014545748, -0.7501189077886223, -0.03474571781315458, 0.6603895951165363]","[0.04407151401699165, 0.16979082390232392, 0.705698973194305]"
---------
observation,tran1,tran2,tran3,rot1,rot2,rot3,rot4,rot5,rot6,rot7,rot8,rot9
1,0.04407151401699165,0.16979082390232392,0.705698973194305,-0.6565323818253673,0.1588616842697038,-0.737379262582055,-0.07928983462501557,-0.9866892288165471,-0.14197683014545748,-0.7501189077886223,-0.03474571781315458,0.6603895951165363
2,0.04407151401699165,0.16979082390232392,0.705698973194305,-0.6565323818253673,0.1588616842697038,-0.737379262582055,-0.07928983462501557,-0.9866892288165471,-0.14197683014545748,-0.7501189077886223,-0.03474571781315458,0.6603895951165363
Because there are commas in the sequences that you want to have
stored, those entries need to be quoted and Python's CSV writer does
that automatically (otherwise the second and following rows of the CSV
would have many more than three elements).
YAML 1.0 was superseded, by YAML 1.1, in 2005 and I have not paid
special attention to the differences between those versions apart from
the directive. YAML 1.2 has been the YAML standard since 2009.ruamel.yaml
only supports files that have explicit YAML 1.2 (or 1.1)
directives, that is why the %YAML:1.0
directive needs to be
explicitly removed from these files.
You might run into trouble if you have any "old-style" octal integers
in your files and in some other cases not shown in your input example.
I stored your example in two differently files with .yaml
endings and then ran:
import sys
from pathlib import Path
import csv
import ruamel.yaml
result = [['observation', 'rotation', 'translation']]
flatres = ["observation,tran1,tran2,tran3,rot1,rot2,rot3,rot4,rot5,rot6,rot7,rot8,rot9".split(',')]
yaml = ruamel.yaml.YAML()
for idx, file_name in enumerate(Path('.').glob('*.yaml')):
txt = file_name.read_text()
if txt.startswith('%YAML:1.0'):
txt = txt.replace('%YAML:1.0', "", 1).lstrip()
data = yaml.load(txt)
result.append([
idx+1,
data['object_rotation_wrt_camera']['data'],
data['object_translation_wrt_camera'],
])
row = [idx+1]
row.extend(data['object_translation_wrt_camera'])
row.extend(data['object_rotation_wrt_camera']['data'])
flatres.append(row)
writer = csv.writer(sys.stdout)
writer.writerows(result)
print('---------')
writer = csv.writer(sys.stdout)
writer.writerows(flatres)
which gives:
observation,rotation,translation
1,"[-0.6565323818253673, 0.1588616842697038, -0.737379262582055, -0.07928983462501557, -0.9866892288165471, -0.14197683014545748, -0.7501189077886223, -0.03474571781315458, 0.6603895951165363]","[0.04407151401699165, 0.16979082390232392, 0.705698973194305]"
2,"[-0.6565323818253673, 0.1588616842697038, -0.737379262582055, -0.07928983462501557, -0.9866892288165471, -0.14197683014545748, -0.7501189077886223, -0.03474571781315458, 0.6603895951165363]","[0.04407151401699165, 0.16979082390232392, 0.705698973194305]"
---------
observation,tran1,tran2,tran3,rot1,rot2,rot3,rot4,rot5,rot6,rot7,rot8,rot9
1,0.04407151401699165,0.16979082390232392,0.705698973194305,-0.6565323818253673,0.1588616842697038,-0.737379262582055,-0.07928983462501557,-0.9866892288165471,-0.14197683014545748,-0.7501189077886223,-0.03474571781315458,0.6603895951165363
2,0.04407151401699165,0.16979082390232392,0.705698973194305,-0.6565323818253673,0.1588616842697038,-0.737379262582055,-0.07928983462501557,-0.9866892288165471,-0.14197683014545748,-0.7501189077886223,-0.03474571781315458,0.6603895951165363
Because there are commas in the sequences that you want to have
stored, those entries need to be quoted and Python's CSV writer does
that automatically (otherwise the second and following rows of the CSV
would have many more than three elements).
YAML 1.0 was superseded, by YAML 1.1, in 2005 and I have not paid
special attention to the differences between those versions apart from
the directive. YAML 1.2 has been the YAML standard since 2009.ruamel.yaml
only supports files that have explicit YAML 1.2 (or 1.1)
directives, that is why the %YAML:1.0
directive needs to be
explicitly removed from these files.
You might run into trouble if you have any "old-style" octal integers
in your files and in some other cases not shown in your input example.
edited Mar 22 at 19:40
answered Mar 22 at 15:34
AnthonAnthon
32.9k1798152
32.9k1798152
This is extremely helpful, thanks so much! Only problem is when I try to specify ['data'] as per your recommendation, I get this error: TypeError: cannot convert dictionary update sequence element #0 to a sequence
– wallZ
Mar 22 at 15:58
If you tried to do the altenternative I proposed, there was an error there, i updated that.
– Anthon
Mar 22 at 16:05
Okay my apologies for being vague. What I would like to do precisely is to store the rotation and translation arrays in one table (or matrix) whereby there are 2 columns, one for rotation and one for translation. Therefore each yaml file would be considered an 'observation', for example, that involves the two variables of rotation and translation.
– wallZ
Mar 22 at 16:16
The translation is alist
, the rotation adict
(and the value fordata
in that dict is again a list. There are no arrays, table, or matrices and I have no idea what you see as difference between these three (nor if any of these three is the same as a Python list). When working with Python you should follow its terminology. Alternatively show YAML/JSON/CSV output of your expected data structure (or how it would look in python) by updating your question (and leave out "EDIT:" or any of that stuff when you do)
– Anthon
Mar 22 at 16:35
Thanks very much for your guidance/advice. I have updated the question and made it much more specific as to my goal I believe.
– wallZ
Mar 22 at 16:54
|
show 4 more comments
This is extremely helpful, thanks so much! Only problem is when I try to specify ['data'] as per your recommendation, I get this error: TypeError: cannot convert dictionary update sequence element #0 to a sequence
– wallZ
Mar 22 at 15:58
If you tried to do the altenternative I proposed, there was an error there, i updated that.
– Anthon
Mar 22 at 16:05
Okay my apologies for being vague. What I would like to do precisely is to store the rotation and translation arrays in one table (or matrix) whereby there are 2 columns, one for rotation and one for translation. Therefore each yaml file would be considered an 'observation', for example, that involves the two variables of rotation and translation.
– wallZ
Mar 22 at 16:16
The translation is alist
, the rotation adict
(and the value fordata
in that dict is again a list. There are no arrays, table, or matrices and I have no idea what you see as difference between these three (nor if any of these three is the same as a Python list). When working with Python you should follow its terminology. Alternatively show YAML/JSON/CSV output of your expected data structure (or how it would look in python) by updating your question (and leave out "EDIT:" or any of that stuff when you do)
– Anthon
Mar 22 at 16:35
Thanks very much for your guidance/advice. I have updated the question and made it much more specific as to my goal I believe.
– wallZ
Mar 22 at 16:54
This is extremely helpful, thanks so much! Only problem is when I try to specify ['data'] as per your recommendation, I get this error: TypeError: cannot convert dictionary update sequence element #0 to a sequence
– wallZ
Mar 22 at 15:58
This is extremely helpful, thanks so much! Only problem is when I try to specify ['data'] as per your recommendation, I get this error: TypeError: cannot convert dictionary update sequence element #0 to a sequence
– wallZ
Mar 22 at 15:58
If you tried to do the altenternative I proposed, there was an error there, i updated that.
– Anthon
Mar 22 at 16:05
If you tried to do the altenternative I proposed, there was an error there, i updated that.
– Anthon
Mar 22 at 16:05
Okay my apologies for being vague. What I would like to do precisely is to store the rotation and translation arrays in one table (or matrix) whereby there are 2 columns, one for rotation and one for translation. Therefore each yaml file would be considered an 'observation', for example, that involves the two variables of rotation and translation.
– wallZ
Mar 22 at 16:16
Okay my apologies for being vague. What I would like to do precisely is to store the rotation and translation arrays in one table (or matrix) whereby there are 2 columns, one for rotation and one for translation. Therefore each yaml file would be considered an 'observation', for example, that involves the two variables of rotation and translation.
– wallZ
Mar 22 at 16:16
The translation is a
list
, the rotation a dict
(and the value for data
in that dict is again a list. There are no arrays, table, or matrices and I have no idea what you see as difference between these three (nor if any of these three is the same as a Python list). When working with Python you should follow its terminology. Alternatively show YAML/JSON/CSV output of your expected data structure (or how it would look in python) by updating your question (and leave out "EDIT:" or any of that stuff when you do)– Anthon
Mar 22 at 16:35
The translation is a
list
, the rotation a dict
(and the value for data
in that dict is again a list. There are no arrays, table, or matrices and I have no idea what you see as difference between these three (nor if any of these three is the same as a Python list). When working with Python you should follow its terminology. Alternatively show YAML/JSON/CSV output of your expected data structure (or how it would look in python) by updating your question (and leave out "EDIT:" or any of that stuff when you do)– Anthon
Mar 22 at 16:35
Thanks very much for your guidance/advice. I have updated the question and made it much more specific as to my goal I believe.
– wallZ
Mar 22 at 16:54
Thanks very much for your guidance/advice. I have updated the question and made it much more specific as to my goal I believe.
– wallZ
Mar 22 at 16:54
|
show 4 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55302302%2fhow-do-i-parse-multiple-files-in-python-and-extract-important-information%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown