Message Boards Message Boards

0
|
3625 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

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

Posted 5 years ago

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

POSTED BY: Jaime de la Mota
2 Replies
Posted 5 years ago

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

POSTED BY: Rohit Namjoshi
Posted 5 years 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
POSTED BY: Rohit Namjoshi
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