Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
VIGNET Pierre
cadbiom
Commits
4baa096e
Commit
4baa096e
authored
Jan 31, 2017
by
VIGNET Pierre
Browse files
Add method to merge cams to csv file
parent
3118a54d
Changes
1
Hide whitespace changes
Inline
Side-by-side
solution_merge.py
0 → 100644
View file @
4baa096e
# -*- coding: utf-8 -*-
from
__future__
import
unicode_literals
from
__future__
import
print_function
import
glob
import
csv
def
merge_cams_to_csv
(
directory
,
csvfile
=
'merged_cams.csv'
):
"""Merge *cam.txt files from a directory to a csv."""
# Add dir separator to the end if not present
directory
=
directory
+
'/'
if
directory
[
-
1
]
!=
'/'
else
directory
csv_data
=
list
()
# Read all files in the given directory
for
filename
in
glob
.
glob
(
directory
+
'*_cam.txt'
):
#print(filename)
# Extract the formula from the filename
# ex:
# # ['./result/Whole NCI-PID database translated into CADBIOM formalism(and)', 'TGFB1', 'cam.txt']
formula
=
''
.
join
(
filename
.
split
(
'_'
)[
1
:
-
1
])
# Read the content of the cam file & memorize this content
with
open
(
filename
)
as
fd
:
# Add formula column, before each cam to futur csv file
csv_data
.
append
([[
formula
]
+
[
line
.
rstrip
(
'
\n
'
)]
for
line
in
fd
])
# Write the final csv
with
open
(
directory
+
csvfile
,
'w'
)
as
fd
:
writer
=
csv
.
writer
(
fd
,
delimiter
=
';'
)
writer
.
writerows
(
*
csv_data
)
if
__name__
==
"__main__"
:
merge_cams_to_csv
(
'result'
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment