Home
Analytics
Use *Space* Delimited Flat File ?
THBG
<p><span>Hello,<br><br>
I'd like to be able to use a *<b>space</b>* delimited flat file into my report.<br>
Unfortunately only comma (CSV), semicolon (SSV), pipe (PSV) and tabulation (TSV) are covered. It seems space is not.<br><br>
I tested TSV but it did not work on my file.<br>
Then my output result is a single column with all my values :<br>
val1 val2 val3 etc...<br><br>
Does anyone know a workaround ?<br>
May with some scripting ?<br><br>
Thanks,<br>
Tom</span></p>
Find more posts tagged with
Comments
Matthew L.
<p>This can be accomplished with a scripted data set by reading in the file line by line, then splitting the lines by their spaces and placing their values into data columns.</p>
<p> </p>
<p>First we read the file in the data set's open method:</p>
<pre class="_prettyXprint _lang-js">
importPackage(Packages.java.io); //Import Java IO
fstream = new FileInputStream(new File("C:/data/SpaceDelimitedValues.txt")); //Create file stream
input = new DataInputStream(fstream); //Create input stream
br = new BufferedReader(new InputStreamReader(input)); //Create buffer reader
str = br.readLine(); //Run once to skip header row in data file
</pre>
<p>Then we can read the file line by line and assign the values into data columns from within the fetch statement:</p>
<pre class="_prettyXprint _lang-js">
str = br.readLine(); //Read next line
if(str){
arr = str.split(" "); //Split line by spaces into array
row["Column1"] = arr[0]; //Use first value
row["Column2"] = arr[1]; //Use second value
row["Column3"] = arr[2]; //Use third value
return true;
}
return false;
</pre>
<p>See attached files for a working example.</p>
THBG
<p>Great Thanks Matthew, this works perfectly.</p>
<p>Is it the same principle if file/data is actually got from URL ?</p>
<p>Thanks again.</p>
<p>Tom</p>
Matthew L.
<p>Yes it's the same principle from a URL.</p>
<p> </p>
<p>Example Data Set open method:</p>
<pre class="_prettyXprint _lang-js">
importPackage(Packages.java.net); //Import Java Net (For URL)
importPackage(Packages.java.io); //Import Java IO
//Get from URL
url = new URL("http://MYDOMAIN.com/FOLDER/SpaceDelimitedValues.txt"); //Create URL object
br = new BufferedReader(new InputStreamReader(url.openStream())); //Create buffer reader
str = br.readLine(); //Run once to skip header row in data file
</pre>
THBG
<p>Many thanks !</p>
<p>Tom</p>