我知道Python并且正在尝试学习Swift ...我试图弄清楚如何从绝对文件路径中读取纯文本文件
在Python中,
#!/usr/bin/python
path= '/users/mu/desktop/test_file.txt'
in_file= open(path,'w').read() #Open the file for reading
for line in in_file.split('\n'): #Split it by line breaks and print the lines
print line
尝试过以下方法:Swift-读取文件得到:“ String.Type没有成员名称stringWithContentsOfFile”
在Python中,这非常容易。必须有一种非常简单的方法来读取文本文件并创建由换行符分隔的字符串列表。
我的问题是如何读取文本文件,以便可以遍历所有行
读取文件后,我可以:
for line in in_file
{
println(line)
}
您可以使用NSFileManager和NSString来解析行,例如:
var path = "somePath"
let fileManager = NSFileManager.defaultManager()
let data:NSData = fileManager.contentsAtPath(path)!
var strs = NSString(data: data, encoding: NSUTF8StringEncoding)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句