Group Abstract Group Abstract

Message Boards Message Boards

0
|
2.1K Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to convert a list into a string of tab separated values

Posted 1 year ago

I have the list:

{
 {195, 0.81},
 {200, 0.33},
 {205, 0.68}
 }

from which I want to create the following string:

"195\t0.81\n200\t0.33\n205\t0.68"

I.e. convert the Wolfram syntax of a two columns table into a string of tab separated values.

Any idea for how to do that?

POSTED BY: Ehud Behar
4 Replies
Posted 1 year ago
POSTED BY: Ehud Behar
Posted 1 year ago

You can do this very directly:

list = {{195, 0.81}, {200, 0.33}, {205, 0.68}};
ExportString[list, "TSV"]
(* "195\t0.81\n200\t0.33\n205\t0.68\n" *)

If you want to trim that trailing newline,

StringTrim[ExportString[list, "TSV"]]

Another option:

StringRiffle[list, "\n", "\t"]
POSTED BY: Eric Rimbey
Posted 1 year ago
POSTED BY: Ehud Behar

One way is to export to tsv format:

Export["~/Desktop/lst.tsv",
 {{195, 0.81}, {200, 0.33}, {205, 0.68}}]
Import["~/Desktop/lst.tsv", "Text"]// InputForm

You can also do it one step at a time:

{{195, 0.81}, {200, 0.33}, {205, 0.68}};
Map[ToString, %, {2}]
Map[Riffle[#, "\t"] &, %]
Riffle[%, "\n"]
% // Flatten
% // StringJoin // InputForm
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard