我有一个文本文件,格式如下。
39.570 -0.246 0.074 1.004 0
39.822 -0.344 0.094 0.887 0
40.083 -0.262 0.234 0.926 0
40.335 -0.457 0.160 0.813 0
40.578 -0.242 0.063 1.008 0
40.830 -0.285 0.172 0.988 0
41.082 -0.277 0.129 0.918 0
41.334 -0.332 0.145 0.996 0
41.586 -0.332 0.156 0.910 0
41.843 -0.340 0.160 0.941 0
42.092 -0.371 0.121 0.793 0
正如您所看到的,将数据拆分为变量没有区别特征。我需要做的是将每一行分成5个单独的变量
e.g。 39.570 -0.246 0.074 1.004 0
将是
var timestamp01 = 39.570
var xaccel01 = -0.246
var yaccel01 = 0.074
var zaccel01 = 1.004
var boardheat01 = 0
并且对于每一行重复该操作直到文档的结尾,其中每个变量具有不同的值,这取决于它所在的行。 timestamp02将与第二行的第一个数字相关,例如39.822
我一直在查找部分信息,但通常最终需要在文本文件中使用标识符(&&和类似的东西)。
这是我到目前为止所做的:
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
movieClip_1.addEventListener(MouseEvent.CLICK, fl_OpenFile);
var fl_OpenFileStream:FileStream = new FileStream(); // FileStream used to read from the file
var fl_OpenFileChooser:File = File.documentsDirectory; // Default to the documents directory
fl_OpenFileChooser.addEventListener(Event.SELECT, fl_FileOpenSelected);
// Main function for opening a file
function fl_OpenFile(event:MouseEvent):void
{
fl_OpenFileChooser.browseForOpen("Select a text file.");
}
// Opens a FileStream object to read the file
function fl_FileOpenSelected(event:Event):void
{
fl_OpenFileChooser = event.target as File;
fl_OpenFileStream = new FileStream();
fl_OpenFileStream.addEventListener(Event.COMPLETE, fl_FileReadHandler);
fl_OpenFileStream.openAsync(fl_OpenFileChooser, FileMode.READ);
}
// Write data from the file to the Output Panel
function fl_FileReadHandler(event:Event):void
{
var fileData:String = fl_OpenFileStream.readMultiByte(fl_OpenFileStream.bytesAvailable, File.systemCharset);
// The data loaded from the file can now be used from the variable fileData.
//Split the string by a newline character
var arLines:Array = fileData.split("n");
var x;
for each (var singleLine:String in arLines)
{
//More code coming up
x = String(arLines).substr(0,6)
// This example code displays data from the file in the Output panel.
outputMod.text = String(x);
}
fl_OpenFileStream.close();
}
x是第一行的第一位数据。你会发现我的编码技巧很低,但只要我有一个起点,我就会很快学会。如果有人能够对这个问题有所了解,或者甚至指出我正确的方向,那就太棒了。
答案 0 :(得分:0)
这将在普通的批处理文件中执行...不确定Actionscript。
我在底部添加了ECHO
行,因此您可以通过检查它将创建的文件Output.txt
来检查输出是否正常工作。
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET sourcefile=list01.txt
SET outputfile=output.txt
:start
SET count=000
FOR /f "tokens=* delims= " %%a IN (%sourcefile%) DO CALL :process1 %%a
PAUSE
GOTO :eof
:process1
SET /a count=%count%+1
SET timestamp%count%=%1
SET xaccel%count%=%2
SET yaccel%count%=%3
SET zaccel%count%=%4
SET boardheat%count%=%5
ECHO Row !count!>>%outputfile%
ECHO Time Stamp:!timestamp%count%! X-accel:!xaccel%count%! Y-accel:!yaccel%count%! Z-accel:!zaccel%count%! Board Heat:!boardheat%count%!>>%outputfile%
GOTO :eof
使用上面的输入列表,此批次的输出文件将如下所示......
Row 1
Time Stamp:39.570 X-accel:-0.246 Y-accel:0.074 Z-accel:1.004 Board Heat:0
Row 2
Time Stamp:39.822 X-accel:-0.344 Y-accel:0.094 Z-accel:0.887 Board Heat:0
Row 3
Time Stamp:40.083 X-accel:-0.262 Y-accel:0.234 Z-accel:0.926 Board Heat:0
Row 4
Time Stamp:40.335 X-accel:-0.457 Y-accel:0.160 Z-accel:0.813 Board Heat:0
Row 5
Time Stamp:40.578 X-accel:-0.242 Y-accel:0.063 Z-accel:1.008 Board Heat:0
Row 6
Time Stamp:40.830 X-accel:-0.285 Y-accel:0.172 Z-accel:0.988 Board Heat:0
Row 7
Time Stamp:41.082 X-accel:-0.277 Y-accel:0.129 Z-accel:0.918 Board Heat:0
Row 8
Time Stamp:41.334 X-accel:-0.332 Y-accel:0.145 Z-accel:0.996 Board Heat:0
Row 9
Time Stamp:41.586 X-accel:-0.332 Y-accel:0.156 Z-accel:0.910 Board Heat:0
Row 10
Time Stamp:41.843 X-accel:-0.340 Y-accel:0.160 Z-accel:0.941 Board Heat:0
Row 11
Time Stamp:42.092 X-accel:-0.371 Y-accel:0.121 Z-accel:0.793 Board Heat:0
答案 1 :(得分:0)
处理看起来相同的项目,你通常希望将它们放在某种可以迭代的容器中(数组/向量)。
在这个解决方案中,我使用了匿名对象,但如果你愿意,可以很容易地将它们扩展到一个类中。
function FileParser() {
var input:String = "39.570 -0.246 0.074 1.004 0\n" + "39.822 -0.344 0.094 0.887 0\n" + "40.083 -0.262 0.234 0.926 0\n" +
"40.335 -0.457 0.160 0.813 0\n" + "40.578 -0.242 0.063 1.008 0\n" + "40.830 -0.285 0.172 0.988 0\n" + "41.082 -0.277 0.129 0.918 0\n" +
"41.334 -0.332 0.145 0.996 0\n" + "41.586 -0.332 0.156 0.910 0\n" + "41.843 -0.340 0.160 0.941 0\n" + "42.092 -0.371 0.121 0.793 0";
var data:Array = new Array();
var rows:Array = input.split("\n");
for (var i:int = 0; i < rows.length; i++)
{
var parts:Array = rows[i].split(" ");
if (parts.length == 5) {
data.push( {
timestamp:parts[0],
xAccel:parts[1],
yAccel:parts[2],
zAccel:parts[3],
boardHeat:parts[4]
});
} else {
throw new Error("Invalid number of values for row:" + rows[i]);
}
}
trace("...iterating over data...");
for (var j:int = 0; j < data.length; j++)
{
trace("timestamp for item #" + j + "=" + data[j].timestamp);
}
}
输出:
...iterating over data...
timestamp for item #0=39.570
timestamp for item #1=39.822
timestamp for item #2=40.083
timestamp for item #3=40.335
timestamp for item #4=40.578
timestamp for item #5=40.830
timestamp for item #6=41.082
timestamp for item #7=41.334
timestamp for item #8=41.586
timestamp for item #9=41.843
timestamp for item #10=42.092
答案 2 :(得分:0)
如果我是你,我会使用RegExp来匹配数据模式。
首先,获取所有线路。以下模式是许多可能的解决方案之一:
var reg1:RegExp = /[\d-. ]*[\n]{1}/g;
var lineArray:Array = yourString.match(reg1);
现在你有一个行数组,如下所示:
39.570 -0.246 0.074 1.004 0
,39.822 -0.344 0.094 0.887 0
,40.083 -0.262 0.234 0.926 0
,40.335 -0.457 0.160 0.813 0
,40.578 -0.242 0.063 1.008 0
,40.830 -0.285 0.172 0.988 0
,41.082 -0.277 0.129 0.918 0
,41.334 -0.332 0.145 0.996 0
,41.586 -0.332 0.156 0.910 0
,41.843 -0.340 0.160 0.941 0
循环通过该数组并为每个循环创建一个保存数据的对象:
for (var i:int = 0; i < lineArray.length; i++)
{
var line:String = lineArray[i];
var dataObject:CustomDataObject = new CustomDataObject(); // create your own class, I named this CustomDataObject
// here, break the line into smaller chunks that are separated by a space
var variableArray:Array = line.split(" ");
dataObject.yourVar1 = Number(variableArray[0]);
dataObject.yourVar2 = Number(variableArray[1]);
dataObject.yourVar3 = Number(variableArray[2]);
dataObject.yourVar4 = Number(variableArray[3]);
dataObject.yourVar5 = Number(variableArray[4]);
}
就是这样!