Lua io.lines("filename", $fmt)

简介:
使用io.lines可以读取文件的内容, 返回一个函数.
每次调用这个函数, 按照格式要求输出文件的内容.
格式如下 : 
"*n": reads a number; this is the only format that returns a number instead of a string.
"*a": reads the whole file, starting at the current position. On end of file, it returns the empty string.
"*l": reads the next line skipping the end of line, returning nil on end of file. This is the default format.
"*L": reads the next line keeping the end of line (if present), returning nil on end of file.
number: reads a string with up to this number of bytes, returning nil on end of file. If number is zero, it reads nothing and returns an empty string, or nil on end of file.

例如 : 
[root@db-172-16-3-150 ~]# pwd
/root
[root@db-172-16-3-150 ~]# ll install.log
-rw-r--r--. 1 root root 41261 Sep 26 15:30 install.log
[root@db-172-16-3-150 ~]# lua
Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio
> f = io.lines("/root/install.log", "*l")  -- "*l"每次读取一行, 不添加回车.
> print(type(f))   -- f是一个函数
function
> print( f() )  -- 调用f函数, 每次返回一行,  不添加回车.
Installing libgcc-4.4.7-3.el6.x86_64
> print( f() )
warning: libgcc-4.4.7-3.el6.x86_64: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
> print( f() )
Installing ca-certificates-2010.63-3.el6_1.5.noarch
> f = io.lines("/root/install.log", "*L")  -- "*L" 每次返回一行, 同时加一个回车.
> print( f() )
Installing libgcc-4.4.7-3.el6.x86_64

> print( f() )
warning: libgcc-4.4.7-3.el6.x86_64: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY

> f = io.lines("/root/install.log", 5)  -- 数字表示每次读取多少个字节, 如果是多字节字符可能有问题.
> print( f() )
Insta
> print( f() )
lling
> f = io.lines("/root/install.log", "*a")  -- "*a" 表示一次读取文件的所有内容
> print( f() )
Installing libgcc-4.4.7-3.el6.x86_64
warning: libgcc-4.4.7-3.el6.x86_64: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Installing ca-certificates-2010.63-3.el6_1.5.noarch
... 略

多字节字符, 使用数字返回, 必须要复合字节数, 否则会有问题.
[root@db-172-16-3-150 ~]# vi test.lua
你好test=100
return test
[root@db-172-16-3-150 ~]# lua
Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio
> f = io.lines("/root/test.lua", 2)
> print( f() )  -- 因为UTF-8中文使用了3个字节, 所以返回有问题.
位
  > print( f() )
?半> 
> print( f() )
¥?
> print( f() )
te
> print( f() )
st
-- 使用3个字节, 正常返回存储的中文.
> f = io.lines("/root/test.lua", 3)
> print( f() )
你
> print( f() )
好
> print( f() )
tes


[参考]
相关文章
|
4月前
|
Go
Go string bytes、strings、strconv和unicode包相关方法
Go string bytes、strings、strconv和unicode包相关方法
26 0
|
4月前
|
Java
Java中的IO(Input/Output)和文件操作
Java中的IO(Input/Output)和文件操作
33 1
|
4月前
npm ERR! code ERR_STREAM_WRITE_AFTER_END npm ERR! write after en
npm ERR! code ERR_STREAM_WRITE_AFTER_END npm ERR! write after en
|
4月前
|
编解码 Python Windows
Python文件路径报错SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 2-3: t
Python文件路径报错SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 2-3: t
|
4月前
记录以下出现:java.io.IOException: (null) entry in command string: null ls -F E:\file\a.txt 情况怎么办?
记录以下出现:java.io.IOException: (null) entry in command string: null ls -F E:\file\a.txt 情况怎么办?
39 0
|
4月前
|
编解码 Python
pandas读取csv错误UnicodeDecodeError: 'utf-8' codec can't decode byte 0xba in position 0: invalid start byte
pandas读取csv错误UnicodeDecodeError: 'utf-8' codec can't decode byte 0xba in position 0: invalid start byte
145 0
|
9月前
|
存储 Shell Python
Python中os.system()、subprocess.run()、call()、check_output()的用法
Python中os.system()、subprocess.run()、call()、check_output()的用法
182 0
在使用xlsx.js导入文件报错Error: Cannot read properties of undefined (reading ‘read‘) at FileReader.read
在使用xlsx.js导入文件报错Error: Cannot read properties of undefined (reading ‘read‘) at FileReader.read
在使用xlsx.js导入文件报错Error: Cannot read properties of undefined (reading ‘read‘) at FileReader.read
|
Go
go并发访问map的坑 fatal error: concurrent map read and map write
go并发访问map的坑 fatal error: concurrent map read and map write
741 0
go并发访问map的坑 fatal error: concurrent map read and map write
|
存储 Linux Python
Python文件相关知识点strip、open、read、write、close、readline、readlines、encoding、encode、decode
文件的作用: 可以永久的保存数据. 文件在硬盘中存储的格式是二进制. 打开文件 读写文件 关闭文件 打开文件, 是文件从硬盘中存到内存中 open(file, mode=‘r’, encoding) file 要操作的文件名字, 类型是 str mode, 文件打开的方式, r(read) 只读打开, w(write) 只写打开 a(append) 追加打开 encoding 文件的编码格式, 常见的编码格式有两种, 一种是gbk, 一种是utf-8 返回值, 文件对象, 后续所有的文件操作,都需要通
284 1
Python文件相关知识点strip、open、read、write、close、readline、readlines、encoding、encode、decode