문제

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.

도움이 되었습니까?

해결책 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.

다른 팁

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).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top