2
$\begingroup$

I have Import a .txt

Data = Import["files.txt", "Table"];

But the Data have the following format, I Cann't get the first column of Data eg. a,b,c, how to get rid of quotation marks in Data?

{{"a,10,20"}, {"b,10,20"}, {"c,10,20"},{"...and so on"}}

I have change the Import format like Data, Elements but all fails.

Any comments will be much appreciate!

$\endgroup$
6
  • 2
    $\begingroup$ Can you share a minimal example of the file in plain text form? While the answer by kglr es excellent, I think it would be nice to have a way to directly interpret the data while importing. $\endgroup$
    – rhermans
    Commented Dec 6, 2021 at 15:58
  • $\begingroup$ @rhermans yes, the minimal example is the Data list in the post, files.txt is very big. The answer by kglr is very helpful, thank you for comments! $\endgroup$
    – lumw
    Commented Dec 6, 2021 at 16:48
  • $\begingroup$ No, what you have in the post is how the data looks like after importing it, not before. Am I wrong? $\endgroup$
    – rhermans
    Commented Dec 6, 2021 at 16:50
  • $\begingroup$ @rhermans right, I have import the .txt data by Import[files.txt, “Table”], the result data has quotation marks. I have try different Import format like “Elements” “Data” but all fails, maybe my txt files have an issue. $\endgroup$
    – lumw
    Commented Dec 6, 2021 at 17:02
  • $\begingroup$ Why don't you share the raw data that we are asking for? Try to make it easier for us. $\endgroup$
    – rhermans
    Commented Dec 6, 2021 at 17:04

3 Answers 3

3
$\begingroup$
data = {{"a,10,20"}, {"b,10,20"}, {"c,10,20"}, {"a,1,1"}};

ReplaceAll[{s_String} :> ToExpression @ StringSplit[s, ","]] @ data
{{a, 10, 20}, {b, 10, 20}, {c, 10, 20}, {a, 1, 1}}
$\endgroup$
6
  • 1
    $\begingroup$ Answer is very useful, thank you so much! $\endgroup$
    – lumw
    Commented Dec 6, 2021 at 16:51
  • $\begingroup$ why we need ToExpression? I have try ReplaceAll[{s_String} :> StringSplit[s, ","]] @ data it still works. $\endgroup$
    – lumw
    Commented Dec 7, 2021 at 1:53
  • 1
    $\begingroup$ @Ben, the results look the same because string quotes are not displayed in output cells. Copy the output from ReplaceAll[{s_String} :> StringSplit[s, ","]] @ data and paste in an input cell, to the same for ReplaceAll[{s_String} :> ToExpression @ StringSplit[s, ","]] @ data and compare. $\endgroup$
    – kglr
    Commented Dec 7, 2021 at 1:58
  • $\begingroup$ Thank you, I see, if not have ToExpression, the output is a string. But if the data = {{"12:30,10,20"}, {"1:30,10,20"}, {"14:30,10,20"}}; ,when elements has a colon, ToExpression will have an issue Invalid syntax in or before "12:30", how can deal with this kind data list? $\endgroup$
    – lumw
    Commented Dec 7, 2021 at 2:12
  • 1
    $\begingroup$ maybe I can use ReplaceAll[{s_String} :> StringSplit[s, ","]] @ data and then use ToExpression to the other number element. I have try it and works. $\endgroup$
    – lumw
    Commented Dec 7, 2021 at 2:20
2
$\begingroup$
data = {{"a,10,20"}, {"b,10,20"}, {"c,10,20"}, {"a,1,1"}};

out1 = ToExpression@StringSplit[#, ","] & @@@ data

out2 = Map[Sequence @@ ToExpression@StringSplit[#, ","] &, data]

{{a, 10, 20}, {b, 10, 20}, {c, 10, 20}, {a, 1, 1}}

$\endgroup$
1
  • $\begingroup$ Thank you give me another concise solution, I will compare the speed of these method in big files data processing. Thank you again for such a good answer! $\endgroup$
    – lumw
    Commented Dec 6, 2021 at 16:56
2
$\begingroup$
list = {{"a,10,20"}, {"b,10,20"}, {"c,10,20"}};

Using ImportString

ToExpression @* ImportString @* ToString /@ list

{{a, 10, 20}, {b, 10, 20}, {c, 10, 20}}

Using Cases

Cases[list, x_ :> Splice @ ToExpression @ StringSplit[x, ","]]

{{a, 10, 20}, {b, 10, 20}, {c, 10, 20}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.