Group Abstract Group Abstract

Message Boards Message Boards

0
|
4.1K Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Parse a .txt file through the Classify function and assign a class?

Posted 7 years ago
POSTED BY: Sam Liu
3 Replies
Posted 7 years ago

Import can only import one file at a time. The import of multiple files happens via the surrounding Map over Range[2]. Break apart the expression to understand what is going on.

Map[Thread["txt" <> # <> ".txt" -> "Gene" <> #] &, ToString /@ Range[2]]

{"txt1.txt" -> "Gene1", "txt2.txt" -> "Gene2"}

Now, instead of "txt1.txt" -> "Gene1" we want the contents of the file "txt1.txt", that is done via the Import and splitting on the two newline characters. If no path is specified, Import will use the default directory. Evaluate Directory[] to see what that is set to on your system. If the files are in a different directory that needs to be specified. e.g.

classes = Join @@ Map[Thread[StringSplit[Import["C:\\filepath\\txt" <> # <> ".txt"], "\n\n"] -> "Gene" <> #] &, ToString /@ Range[2]];

For an introduction to how Map and pure functions work, take a look at this and this.

POSTED BY: Rohit Namjoshi
POSTED BY: Sam Liu
Posted 7 years ago

The argument passed to Import is incorrect. Try this:

classes = Join @@ Map[Thread[StringSplit[Import["txt" <> # <> ".txt"], "\n\n"] -> "Gene" <> #] &, ToString /@ Range[2]];

classes // Short
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