aardio操作文本文件的笔记
本帖最后由 rmajly 于 2024-1-26 21:50 编辑aardio开发笔记:文本类
1、一次逐行读出txt文本
import console;
for lineTxt in io.lines("C:\Users\Administrator\Desktop\aaa.txt"){
console.log(lineTxt)
}
console.pause(true);
2、把文本每行以空隔为界,逐行读到listview里
import win.ui;
//import string;
/*DSG{{*/
var winform = win.form(text="aardio Form";right=459;bottom=372;parent=...)
winform.add(
listview={cls="listview";left=6;top=33;right=452;bottom=253;bgcolor=16777215;edge=1;z=1}
)
/*}}*/
winform.listview.insertColumn("序号",40)
winform.listview.insertColumn("测试",200)
winform.listview.insertColumn("测试",200)
winform.listview.clear();
var x = 1;
for line in io.lines("C:\Users\Administrator\Desktop\aaa.txt") { //采取分行迭代方法读取test.txt文本的内容;
var a,b = table.unpack(string.split(line,"< >")); //string.split以“ ”为分割符分割读取的文本行 然后把分开的值分别赋值给a,b,模式匹配详细请参考帮助文档和论坛;
winform.listview.addItem({tostring(x);a;b;});//显示到listview控件;
x++;
}
winform.show()
win.loopMessage();
3、查找txt内文本的最大行数
io.open();//打开控制台
var lines = 0;
for line in io.lines("C:\Users\Administrator\Desktop\aaa.txt") {
lines++;
}
io.print("您输入的是" ,lines );
execute("pause") //按任意键继续
4、这个代码就有意思了,上面读数据,最后写总行数;
import console;
var lines=0
for line in io.lines("C:\Users\Administrator\Desktop\aaa.txt"){
console.log(line)//这是逐行读的总的数据
lines++
}
console.log(lines)//这是总的行数
console.pause(true);
5、(1)文本分割
import console;
var a,b = table.unpack(string.split("apple banana"," "))
console.log(a)
console.log(b)
execute("pause") //按任意键继续
(2)字符拼接
io.open(); //打开控制台窗口
str = "12345678";
t = string.split(str); //按单个字符开始拆分
io.print(tostring(t)++tostring(t)++tostring(t) +"*");
execute("pause") //按任意键继续
io.close();//关闭控制台
6、查找某个数据在等几行
import console;
var num = {}
for line in io.lines("C:\Users\Administrator\Desktop\123.txt") {
table.push(num,line)
if( string.find(line,"李四") ){
console.log("发现李四在第",#num,"行")
}
}
execute("pause") //按任意键继续
7、找某个关键字,并找出空格前、后的数据
import console;
for line in io.lines("C:\Users\Administrator\Desktop\123.txt") {
var a,b = table.unpack(string.split(line,"< >"));
if( string.find(line,"001") ){
console.log("找到同行空格后的数据为",b)
console.log("找到同行空格前的数据为",a)
}
}
execute("pause") //按任意键继续
页:
[1]