Question

Does anyone know how to read a .txt file from Mathematica in lines and save those lines as elements from an array? The file I'm trying to read are names like this:

  • Brad Pitt
  • Scarlet Johanson
  • Woody Allen
  • etc

I have no experience with Mathematica by the way, thanks.

Was it helpful?

Solution 2

If you are on Windows and your text file is named C:\dir\names.txt, then you can use

names = StringSplit[Import["C:/dir/names.txt"], "\n"]

to import the file, and then split the resulting string at the end of each new line into an array of strings. Note the different direction of the slashes.

If you aren't on Windows, the command is the same, but the directory won't be prefixed with a drive letter.

OTHER TIPS

ReadList["filename.txt", String] is the command that reads a file called filename.txt and puts each line, as a string, into a List (which is an array).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top