Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
gargantext
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
humanities
gargantext
Commits
e8bde84e
Commit
e8bde84e
authored
Mar 30, 2016
by
Romain Loth
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
CSV file parsing re-activated
parent
cc569bbf
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
25 additions
and
32 deletions
+25
-32
constants.py
gargantext/constants.py
+4
-4
CSV.py
gargantext/util/parsers/CSV.py
+20
-27
__init__.py
gargantext/util/parsers/__init__.py
+1
-1
No files found.
gargantext/constants.py
View file @
e8bde84e
...
@@ -84,10 +84,10 @@ RESOURCETYPES = [
...
@@ -84,10 +84,10 @@ RESOURCETYPES = [
'parser'
:
RISParser
,
'parser'
:
RISParser
,
'default_language'
:
'en'
,
'default_language'
:
'en'
,
},
},
#
{ 'name': 'CSV',
{
'name'
:
'CSV'
,
# #
'parser': CSVParser,
'parser'
:
CSVParser
,
#
'default_language': 'en',
'default_language'
:
'en'
,
#
},
},
# { 'name': 'ISTex',
# { 'name': 'ISTex',
# # 'parser': ISTexParser,
# # 'parser': ISTexParser,
# 'default_language': 'en',
# 'default_language': 'en',
...
...
gargantext/util/parsers/CSV
Parser
.py
→
gargantext/util/parsers/CSV.py
View file @
e8bde84e
...
@@ -8,34 +8,32 @@ import os
...
@@ -8,34 +8,32 @@ import os
class
CSVParser
(
Parser
):
class
CSVParser
(
Parser
):
def
CSVsample
(
self
,
filename
,
delim
)
:
def
CSVsample
(
self
,
small_contents
,
delim
)
:
ifile
=
open
(
filename
,
"r"
)
reader
=
csv
.
reader
(
small_contents
,
delimiter
=
delim
)
reader
=
csv
.
reader
(
ifile
,
delimiter
=
delim
)
Freqs
=
[]
Freqs
=
[]
for
row
in
reader
:
for
row
in
reader
:
Freqs
.
append
(
len
(
row
))
Freqs
.
append
(
len
(
row
))
ifile
.
close
()
return
Freqs
return
Freqs
def
parse
(
self
,
filename
):
def
parse
(
self
,
filename
):
print
(
"CSV: parsing (assuming UTF-8 and LF line endings)"
)
contents
=
filename
.
read
()
.
decode
(
"UTF-8"
)
.
split
(
"
\n
"
)
sample_size
=
10
sample_size
=
10
sample_
file
=
filename
.
replace
(
".csv"
,
"_sample.csv"
)
sample_
contents
=
contents
[
0
:
sample_size
]
hyperdata_list
=
[]
hyperdata_list
=
[]
command_for_sample
=
"cat '"
+
filename
+
"' | head -n "
+
str
(
sample_size
)
+
" > '"
+
sample_file
+
"'"
os
.
system
(
command_for_sample
)
# you just created a *_sample.csv
# # = = = = [ Getting delimiters frequency ] = = = = #
# # = = = = [ Getting delimiters frequency ] = = = = #
PossibleDelimiters
=
[
','
,
' '
,
'
\t
'
,
';'
,
'|'
,
':'
]
PossibleDelimiters
=
[
','
,
' '
,
'
\t
'
,
';'
,
'|'
,
':'
]
AllDelimiters
=
{}
AllDelimiters
=
{}
for
delim
in
PossibleDelimiters
:
for
delim
in
PossibleDelimiters
:
AllDelimiters
[
delim
]
=
self
.
CSVsample
(
sample_
file
,
delim
)
AllDelimiters
[
delim
]
=
self
.
CSVsample
(
sample_
contents
,
delim
)
# # = = = = [ / Getting delimiters frequency ] = = = = #
# # = = = = [ / Getting delimiters frequency ] = = = = #
# # OUTPUT example:
# # OUTPUT example:
# # AllDelimiters = {
# # AllDelimiters = {
...
@@ -59,8 +57,8 @@ class CSVParser(Parser):
...
@@ -59,8 +57,8 @@ class CSVParser(Parser):
# # = = = = [ / Stand.Dev=0 & Sum of delimiters ] = = = = #
# # = = = = [ / Stand.Dev=0 & Sum of delimiters ] = = = = #
# # OUTPUT example:
# # OUTPUT example:
# # Delimiters = [
# # Delimiters = [
# # ['\t', 5, 5, 0.0],
# # ['\t', 5, 5, 0.0],
# # [',', 75, 5, 0.0],
# # [',', 75, 5, 0.0],
# # ['|', 5, 5, 0.0]
# # ['|', 5, 5, 0.0]
# # ]
# # ]
...
@@ -68,23 +66,22 @@ class CSVParser(Parser):
...
@@ -68,23 +66,22 @@ class CSVParser(Parser):
# # = = = = [ Delimiter selection ] = = = = #
# # = = = = [ Delimiter selection ] = = = = #
Sorted_Delims
=
sorted
(
Delimiters
,
key
=
lambda
x
:
x
[
1
],
reverse
=
True
)
Sorted_Delims
=
sorted
(
Delimiters
,
key
=
lambda
x
:
x
[
1
],
reverse
=
True
)
HighestDelim
=
Sorted_Delims
[
0
][
0
]
HighestDelim
=
Sorted_Delims
[
0
][
0
]
#
print("selected delimiter:",[HighestDelim]
#
HighestDelim = ","
# print
print
(
"CSV selected delimiter:"
,[
HighestDelim
])
# # = = = = [ / Delimiter selection ] = = = = #
# # = = = = [ / Delimiter selection ] = = = = #
# # = = = = [ First data coordinate ] = = = = #
# # = = = = [ First data coordinate ] = = = = #
Coords
=
{
Coords
=
{
"row"
:
-
1
,
"row"
:
-
1
,
"column"
:
-
1
"column"
:
-
1
}
}
ifile
=
open
(
sample_file
,
"r"
)
reader
=
csv
.
reader
(
contents
,
delimiter
=
HighestDelim
)
reader
=
csv
.
reader
(
ifile
,
delimiter
=
HighestDelim
)
for
rownum
,
tokens
in
enumerate
(
reader
):
for
rownum
,
tokens
in
enumerate
(
reader
):
if
rownum
%
250
==
0
:
print
(
"CSV row: "
,
rownum
)
joined_tokens
=
""
.
join
(
tokens
)
joined_tokens
=
""
.
join
(
tokens
)
if
Coords
[
"row"
]
<
0
and
len
(
joined_tokens
)
>
0
:
if
Coords
[
"row"
]
<
0
and
len
(
joined_tokens
)
>
0
:
Coords
[
"row"
]
=
rownum
Coords
[
"row"
]
=
rownum
...
@@ -93,22 +90,21 @@ class CSVParser(Parser):
...
@@ -93,22 +90,21 @@ class CSVParser(Parser):
if
len
(
t
)
>
0
:
if
len
(
t
)
>
0
:
Coords
[
"column"
]
=
columnum
Coords
[
"column"
]
=
columnum
break
break
ifile
.
close
()
# # = = = = [ / First data coordinate ] = = = = #
# # = = = = [ / First data coordinate ] = = = = #
# # = = = = [ Setting Headers ] = = = = #
# # = = = = [ Setting Headers ] = = = = #
Headers_Int2Str
=
{}
Headers_Int2Str
=
{}
ifile
=
open
(
sample_file
,
"r"
)
reader
=
csv
.
reader
(
contents
,
delimiter
=
HighestDelim
)
reader
=
csv
.
reader
(
ifile
,
delimiter
=
HighestDelim
)
for
rownum
,
tokens
in
enumerate
(
reader
):
for
rownum
,
tokens
in
enumerate
(
reader
):
if
rownum
>=
Coords
[
"row"
]:
if
rownum
>=
Coords
[
"row"
]:
for
columnum
in
range
(
Coords
[
"column"
],
len
(
tokens
)
):
for
columnum
in
range
(
Coords
[
"column"
],
len
(
tokens
)
):
t
=
tokens
[
columnum
]
t
=
tokens
[
columnum
]
Headers_Int2Str
[
columnum
]
=
t
Headers_Int2Str
[
columnum
]
=
t
break
break
ifile
.
close
()
# print("Headers_Int2Str")
# print(Headers_Int2Str)
# # = = = = [ / Setting Headers ] = = = = #
# # = = = = [ / Setting Headers ] = = = = #
# # OUTPUT example:
# # OUTPUT example:
# # Headers_Int2Str = {
# # Headers_Int2Str = {
...
@@ -119,11 +115,9 @@ class CSVParser(Parser):
...
@@ -119,11 +115,9 @@ class CSVParser(Parser):
# # }
# # }
# # = = = = [ Reading the whole CSV and saving ] = = = = #
# # = = = = [ Reading the whole CSV and saving ] = = = = #
hyperdata_list
=
[]
hyperdata_list
=
[]
ifile
=
open
(
filename
,
"r"
)
reader
=
csv
.
reader
(
contents
,
delimiter
=
HighestDelim
)
reader
=
csv
.
reader
(
ifile
,
delimiter
=
HighestDelim
)
for
rownum
,
tokens
in
enumerate
(
reader
):
for
rownum
,
tokens
in
enumerate
(
reader
):
if
rownum
>
Coords
[
"row"
]:
if
rownum
>
Coords
[
"row"
]:
RecordDict
=
{}
RecordDict
=
{}
...
@@ -131,7 +125,6 @@ class CSVParser(Parser):
...
@@ -131,7 +125,6 @@ class CSVParser(Parser):
data
=
tokens
[
columnum
]
data
=
tokens
[
columnum
]
RecordDict
[
Headers_Int2Str
[
columnum
]
]
=
data
RecordDict
[
Headers_Int2Str
[
columnum
]
]
=
data
hyperdata_list
.
append
(
RecordDict
)
hyperdata_list
.
append
(
RecordDict
)
ifile
.
close
()
# # = = = = [ / Reading the whole CSV and saving ] = = = = #
# # = = = = [ / Reading the whole CSV and saving ] = = = = #
return
hyperdata_list
return
hyperdata_list
gargantext/util/parsers/__init__.py
View file @
e8bde84e
...
@@ -8,4 +8,4 @@ from .Pubmed import PubmedParser
...
@@ -8,4 +8,4 @@ from .Pubmed import PubmedParser
from
.Europress
import
EuropressParser
from
.Europress
import
EuropressParser
# from .ISTex import ISTexParser
# from .ISTex import ISTexParser
#
from .CSV import CSVParser
from
.CSV
import
CSVParser
Write
Preview
Markdown
is supported
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