读取txt文件,并按条件生成新的文件
一个1.txt文件,里面数据格式如下:
现按时间将其重新分配,按时间生成相应的文件(如下,将2002-4-1的数据放到一个文件中,并命名为2002-4-1.txt,则如下数据生成3个新的文件:2002-4-1.txt、2002-4-2.txt、2002-4-3.txt)
52546 2002-4-117.64.321444.2
522672002-4-122.94.416640.1
526522002-4-117.83.618443.7
525462002-4-217.73.919344
522672002-4-220.24.318836.5
526522002-4-216.84.524243.4
525462002-4-39.37.363722.2
522672002-4-312.66.545314.6
526522002-4-36.87.980410.5
生成的新文件时间列去掉,其他数据按原格式存放。例如:2002-4-1.txt文件为:
52546 17.64.321444.2
5226722.94.416640.1
5265217.83.618443.7
[解决办法]
'假如日期是排序好的,供参考
CommonDialog1.FileName = "1.TXT "
Open CommonDialog1.FileName For Input As #1
Line Input #1, temp '读文件
temp0=split(temp, " ")
biao=temp0(1) '取得时间字段
mark1:
CommonDialog2.FileName = biao+ ".txt " '打开输出文件
Open CommonDialog2.FileName For Output As #2
mark2:
print #2,temp0(0)+ " "+temp0(2)+ " "+temp0(3)+ " "+temp(4)+ " "+temp0(5)+ " "+temp0(6) '按格式输出
If Not EOF(1) Then
Line Input #1, temp '读1.txt文件
temp0=split(temp, " ")
if biao=temp0(1) then
goto mark2 '如果时间相同继续输出
else
close #2 '如果时间不同,关闭前一个文件,对第二个文件更名并重新打开
biao=temp0(1)
goto mark1
end if
else
close #1
close #2
end if
[解决办法]
strFileName1 = "E:\VB练习\文件操作 " & "\ " & "2002- " & i & "- " & j + ".txt "
i,j 为数值型 应该加str(i),str(j)
strFileName1 = "E:\VB练习\文件操作 " & "\ " & "2002- " & str(i) & "- " & str(j) + ".txt "