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

Posted 8 days ago
98 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.
Answer
2 Replies
Sort By:
Posted 8 days ago
 Avoid extra comma while importing a text file as a vector?
Answer
Posted 8 days 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 
Answer
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments