python-jgrapht
python-jgrapht copied to clipboard
CSV edgelist import: mismatched input
I'm trying to import an edgelist file with no success.
My input file is a single edge; the file is:
0, 1
My code is as follows (<input file> substitutes for the file path):
import jgrapht as j
input_file = <input file>
e = j.io.edgelist.parse_edgelist_csv(input_file, format='edgelist')
I get the following error:
Traceback (most recent call last):
File "<python file>", line 12, in <module>
e = j.io.edgelist.parse_edgelist_csv(input_file, format='edgelist')
File "<....>/jgrapht/io/edgelist.py", line 532 in parse_edgelist_csv
return _import_edgelist_with_string_ids("string_csv", False, input_string, *args)
File "<....>/jgrapht/io/edgelist.py", line 23 in _import_edgelist_with_string_ids
res = alg_method(filename_or_string_as_bytearray, *args)
File "<....>/jgrapht/backend.py", line 780, in jgrapht_import_edgelist_noattrs_string_csv
return _backend_jgrapht_import_edgelist_noattrs_string_csv(BYTEARRAY, arg2, arg3, arg4, arg5)
', 'ror: Failed to import CSV graph: line 1:38 mismatched innput '<EOF>' expecting {'
', SEPARATOR}
I'm running this in a conda environment, Python 3.8, with no other installed packages than those installed when installing jgrapht, on an Ubuntu VM.
It is a bit counter-intuitive but we are using https://datatracker.ietf.org/doc/html/rfc4180 which is a bit strict. You need to remove the space between the two values. If you write "0,1" is should work.
I probably need to relax this, at some point.
Unfortunately this doesn't fix my issue - same error. I checked the linked spec and it looks like it might want a space at the end of the line, so I tried that as well just in case it wasn't for human-readability (no dice). Since the file is only one line long, the lack of CRLF at the end of the line shouldn't matter.
Edit: adding CRLF at the end also does not fix; it gives the same error, but a slightly different column number, I assume due to the longer line.
You are right, we have a bug in our grammar. It assumes that your input has at least two lines and that each line ends with a \n
, even the last one. For now, if you have a one-line input, you should parse it by hand and add the edge manually using g.add_edge()
.
One minor issue as well is to use read_edgelist_csv
if you are trying to read from file, instead of parse_edgelist_csv
which reads from a string.