Message Boards Message Boards

GROUPS:

Avoid extra comma while importing a text file as a vector?

Posted 2 months ago
289 Views
|
2 Replies
|
0 Total Likes
|

Hello everyone. I am currently having some issues using Import on a txt file which is a Python output. When I import said file as a vector, a comma appears before one of the elements, making it impossible to work with said vector. I will adjoint images of my problem in case someone can aid me.

Thanks in advance.enter image description here

enter image description here

2 Replies
Posted 2 months ago

Avoid extra comma while importing a text file as a vector?

Posted 2 months ago

Problem is caused by the \n (newline) characters. The Table format for Import uses \n to delimit rows. Can you modify the Python code to not insert a newline between [ ]? If not, probably best to use sed or awk to remove those newlines. You could also import as a string and munge it using WL. e.g.

import = Import["nodes.txt", "Text"];
string = import // StringReplace[{"\n" -> " ", "[" -> "{", "]" -> "}"}] // StringReplace[" " -> ","];
list = "{" <> string <> "}" // ToExpression
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract