How to read and select only a specific lines from a CSV file. Pls help
I have a java application to read a CSV file. I've already managed to read them all and output it to System.out.println(). Now the problem is I want to select a specific row of the records. How do I do it? Below is the codes i've typed.
FileReader fr = new FileReader(fileName);
BufferedReader inFile = new BufferedReader(fr);
StringBuffer buf = new StringBuffer();
String line = null;
while ((line = inFile.readLine()) != null) {
buf.append(line);
buf.append("\n");
String inString = buf.toString();
System.out.println(inString);
This is the record that i want to read. For example, I want to select n print out line "80,2.90,3.00,3.10,3.20,......."
[Factor1]
Time [s],0.00,0.10,0.20,0.30,0.40,0.50,0.60,0.70,0.80,0.90,1.00,1.10,1.20,1.30,1.40,1.50,1.60,1.70,1.80,1.90,2.00,2.10,2.20,2.30,2.40,2.50,2.60,2.70,2.80,2.90,3.00,3.10,3.20,3.30,3.40,3.50,3.60,3.70,3.80,3.90,4.00,4.10,4.20,4.30,4.40,4.50,4.60,4.70,4.80,4.90,5.00,5.10,5.20,5.30,5.40,5.50,5.60,5.70,5.80,5.90,6.00,6.10,6.20,6.30,6.40,6.50,6.60,6.70,6.80,6.90,7.00,7.10,7.20,7.30
Phi-0 [dB],-0.86,-0.80,-0.80,-0.99,-0.56,-0.53,-0.95
If you know how many bytes each line is, you can use RandomAccessFile to skip directly to the byte position corresponding to your desired line. Otherwise, you'll have to read each line and just ignore the ones you don't want.
Similar Messages
-
How can I use Automator to extract specific Data from a text file?
I have several hundred text files that contain a bunch of information. I only need six values from each file and ideally I need them as columns in an excel file.
How can I use Automator to extract specific Data from the text files and either create a new text file or excel file with the info? I have looked all over but can't find a solution. If anyone could please help I would be eternally grateful!!! If there is another, better solution than automator, please let me know!
Example of File Contents:
Link Time =
DD/MMM/YYYY
Random
Text
161 179
bytes of CODE memory (+ 68 range fill )
16 789
bytes of DATA memory (+ 59 absolute )
1 875
bytes of XDATA memory (+ 1 855 absolute )
90 783
bytes of FARCODE memory
What I would like to have as a final file:
EXCEL COLUMN1
Column 2
Column3
Column4
Column5
Column6
MM/DD/YYYY
filename1
161179
16789
1875
90783
MM/DD/YYYY
filename2
xxxxxx
xxxxx
xxxx
xxxxx
MM/DD/YYYY
filename3
xxxxxx
xxxxx
xxxx
xxxxx
Is this possible? I can't imagine having to go through each and every file one by one. Please help!!!Hello
You may try the following AppleScript script. It will ask you to choose a root folder where to start searching for *.map files and then create a CSV file named "out.csv" on desktop which you may import to Excel.
set f to (choose folder with prompt "Choose the root folder to start searching")'s POSIX path
if f ends with "/" then set f to f's text 1 thru -2
do shell script "/usr/bin/perl -CSDA -w <<'EOF' - " & f's quoted form & " > ~/Desktop/out.csv
use strict;
use open IN => ':crlf';
chdir $ARGV[0] or die qq($!);
local $/ = qq(\\0);
my @ff = map {chomp; $_} qx(find . -type f -iname '*.map' -print0);
local $/ = qq(\\n);
# CSV spec
# - record separator is CRLF
# - field separator is comma
# - every field is quoted
# - text encoding is UTF-8
local $\\ = qq(\\015\\012); # CRLF
local $, = qq(,); # COMMA
# print column header row
my @dd = ('column 1', 'column 2', 'column 3', 'column 4', 'column 5', 'column 6');
print map { s/\"/\"\"/og; qq(\").$_.qq(\"); } @dd;
# print data row per each file
while (@ff) {
my $f = shift @ff; # file path
if ( ! open(IN, '<', $f) ) {
warn qq(Failed to open $f: $!);
next;
$f =~ s%^.*/%%og; # file name
@dd = ('', $f, '', '', '', '');
while (<IN>) {
chomp;
$dd[0] = \"$2/$1/$3\" if m%Link Time\\s+=\\s+([0-9]{2})/([0-9]{2})/([0-9]{4})%o;
($dd[2] = $1) =~ s/ //g if m/([0-9 ]+)\\s+bytes of CODE\\s/o;
($dd[3] = $1) =~ s/ //g if m/([0-9 ]+)\\s+bytes of DATA\\s/o;
($dd[4] = $1) =~ s/ //g if m/([0-9 ]+)\\s+bytes of XDATA\\s/o;
($dd[5] = $1) =~ s/ //g if m/([0-9 ]+)\\s+bytes of FARCODE\\s/o;
last unless grep { /^$/ } @dd;
close IN;
print map { s/\"/\"\"/og; qq(\").$_.qq(\"); } @dd;
EOF
Hope this may help,
H -
Having trouble reading specific lines from a text file and displaying them in a listbox
I am trying to read specific lines from all of the text files in a folder that are reports. When I run the application I get the information from the first text file and then it returns this error: "A first chance exception of type 'System.ArgumentOutOfRangeException'
occurred in mscorlib.dll"
Below is the code from that form.
Option Strict On
Option Infer Off
Option Explicit On
Public Class frmInventoryReport
Public Function ReadLine(ByVal lineNumber As Integer, ByVal lines As List(Of String)) As String
Dim intTemp As Integer
intTemp = lineNumber
Return lines(lineNumber - 1)
lineNumber = intTemp
End Function
Public Function FileMatches(ByVal folderPath As String, ByVal filePattern As String, ByVal phrase As String) As Boolean
For Each fileName As String In IO.Directory.GetFiles(folderPath, filePattern)
If fileName.ToLower().Contains(phrase.ToLower()) Then
Return True
End If
Next
Return False
End Function
Private Sub frmInventoryReport_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim intcase As Integer = 1
Dim strTemp, strlist, strFile As String
Dim blnCheck As Boolean = True
strFile = "Report Q" & intcase.ToString & ".txt"
Do While blnCheck = True
strFile = "Report Q" & intcase.ToString & ".txt"
Dim objReader As New System.IO.StreamReader("E:\Furry Friends Animal Shelter Solution\Furry Friends Animal Shelter\" & strFile)
Dim allLines As List(Of String) = New List(Of String)
Do While objReader.Peek <> -1
allLines.Add(objReader.ReadLine())
Loop
objReader.Close()
strlist = ReadLine(1, allLines) & "" & ReadLine(23, allLines)
lstInventory.Items.Add(strlist)
intcase += 1
strTemp = intcase.ToString
strFile = "Report Q" & intcase.ToString & ".txt"
blnCheck = FileMatches("E:\Furry Friends Animal Shelter Solution\Furry Friends Animal Shelter\", "*.txt", intcase.ToString)
Loop
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim intcase As Integer = 1
Dim strTemp, strlist, strFile As String
Dim blnCheck As Boolean = True
strFile = "Report Q" & intcase.ToString & ".txt"
Do While blnCheck = True
strFile = "Report Q" & intcase.ToString & ".txt"
Dim objReader As New System.IO.StreamReader("E:\Furry Friends Animal Shelter Solution\Furry Friends Animal Shelter\" & strFile)
Dim allLines As List(Of String) = New List(Of String)
Do While objReader.Peek <> -1
allLines.Add(objReader.ReadLine())
Loop
objReader.Close()
strlist = ReadLine(1, allLines) & "" & ReadLine(23, allLines)
lstInventory.Items.Add(strlist)
intcase += 1
strTemp = intcase.ToString
strFile = "Report Q" & intcase.ToString & ".txt"
blnCheck = FileMatches("E:\Furry Friends Animal Shelter Solution\Furry Friends Animal Shelter\", "*.txt", intcase.ToString)
Loop
End Sub
End Class
Sorry I'm just beginning coding and I'm still a noob. Any help is appreciated. Thank you!Ok, so if I'm following this correctly you should be able to just loop through all of the files in that folder whose file name matches the pattern and then read the first 22 lines, recording only the first and the last.
Exactly how you store the animal data probably depends on how you are going to display it and what else you are going to do with it. Is there anything other than name and cage number that should be associated with each animal?
You might want to make a dataset with a datatable to describe the animal, or you might write a class, or you might just use something generic like a Tuple. Here's a simple class example:
Public Class Animal
Public Property Name As String
Public Property Cage As String
Public Overrides Function ToString() As String
Return String.Format("{0} - {1}", Name, Cage)
End Function
End Class
With that you can use a routine like the following to loop through all of the files and read each one:
Dim animals As New List(Of Animal)
Dim folderPath As String = "E:\Furry Friends Animal Shelter Solution\Furry Friends Animal Shelter\"
For Each filePath As String In System.IO.Directory.GetFiles(folderPath, "Report Q?.txt")
Using reader As New System.IO.StreamReader(filePath)
Dim lineIndex As Integer = 0
Dim currentAnimal As New Animal
While Not reader.EndOfStream
Dim line As String = reader.ReadLine
If lineIndex = 0 Then
currentAnimal.Name = line
ElseIf lineIndex = 22 Then
currentAnimal.Cage = line
Exit While
End If
lineIndex += 1
End While
animals.Add(currentAnimal)
End Using
Next
'do something to display the animals list
Then you might bind the animals list to a ListBox, or loop through the list and populate a ListView. If you decided to fill a datatable instead of making Animal instances, then you might bind the resulting table to a DataGridView.
There are lots of options depending on what you want and what all you need to do.
Reed Kimble - "When you do things right, people won't be sure you've done anything at all" -
SQL*Loader - How to load only a few columns from a .csv file to ora table
Hi there,
Could anyone please let me know how to load few columns from a .csv
file into a oracle table using SQL*Loader.
I know how to create a .dat and .ctl file and run the sql loader.
Suppose I have a .csv file with
col1, col2, col3, col4
and I only need to load col1 and col3 into col_a and col_b respectively
in table_a?
structure of table_ a
col_a,
col_b
Please adviceTry like..it i will work..
LOAD DATA
INFILE 'test.txt'
LOAD DATA
TRUNCATE INTO TABLE T1
FIELDS TERMINATED BY ','
(col1,
col2 FILLER,
col3,
col4 FILLER
) -
How do I remove or unregister my iphone. Changed to Samsung s4 and now cannot receive text messages from iphone users. Pls help
http://support.apple.com/kb/ts5185
-
Select only one specific value from two values contained in an item in form
hi ,
i am newly creating a form.
in the all item i an displaying on control block,
in one item "dummy.formule" i am taking the value as '1-xyz' where 1 is nummer and xyz is naam
or '99-fgj'.
but for the next block i want to write a query in which in the where condition i need to use the nummer from dummy.formule.this query is dymannically created at run time.
i am wrting this query in the when validate of the "dummy.formule" .
how i get the specific value that i required?923315 wrote:
hi ,
i am newly creating a form.
in the all item i an displaying on control block,
in one item "dummy.formule" i am taking the value as '1-xyz' where 1 is nummer and xyz is naam
or '99-fgj'.
but for the next block i want to write a query in which in the where condition i need to use the nummer from dummy.formule.this query is dymannically created at run time.
i am wrting this query in the when validate of the "dummy.formule" .
how i get the specific value that i required?First look at it. dynamically create pop list at run time. close it.
what you mean by "naam" ?
If the value has a specific format like as you write "1-xyz,99-fgj". you can use instr function and substr function to cut it out..
Hope this helps. -
Hello,
I am working with other groups and getting the data in daily .csv files. When I use the "concatenate groups" script along with a script on importing files, I end up getting a huge file that takes about an hour to concatenate. In order to reduce the amount of time and memory that this takes, I was hoping that someone could help me modify the script so that I could just list the channel name that I am interested in and concatinating only those channels rather than all of them.
For example, if voltage, temperature, pressure and time data are taken daily for 30 days, I would like to import only the temperature and time data (from .csv format) and concatenate into one group.
I have attached the .vbs files that I use.
Thanks in advance,
Alan
Attachments:
Import and concatenate files.zip 9 KBHi Alan,
Actually, the feature you're asking for is already in the code of mine that you sent back. Look on line 11 of the main VBScript: ChannelSet = "" ' "" or "1-" (DataPlugin) or "Sheet1" (EXCEL Wizard)
If this "ChannelSet" parameter is set to something other than "" or "1-" then it is used in line 78:
Call DataFileLoadSel(FilePaths(i), DataPlugin, ChannelSet)
Yo can specify the channel indices to load with an expression like this:
ChannelSet = "[1]/[1],[3]"
Let me know if you have further questions,
Brad Turpin
DIAdem Product Support Engineer
National Instruments -
How to read the content in one node of XML in Java? Pls help
My dear brothers,
I am a newbie of XML, I have a exercise which is creating a Tree View from XML file. But the trouble is I do not know how to read the content in one node of XML file. I decide to use the algorithm as following:
1. Create a GUI form which gives the ability for user to choose a XML file (ok)
2. Load XML and return the file (ok)
3. Read the file from node to node to create the node in Tree View (?!)
Please help me, and if you are enough kind, please give me an small example to easy understand. Thanks in advance.
Hoang Yen BinhI hope this one helps you.
<ABC Type="ProductBased" ProdName="One" Location="India">
<CEO>Raj</CEO>
<Finance>Vikram</Finance>
<HR>Karthik</HR>
<Technical>Satish</Technical>
</ABC>
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Attr;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.DOMException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import java.io.File;
import java.io.IOException;
public class XmlReading {
Document doc;
Element element;
public static void main(String[] args) throws Exception{
XmlReading xr = new XmlReading();
xr.getXmlParser(args);
public void getXmlParser(String[] args) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
if(args.length != 1) {
System.err.println("Argument Required");
try {
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.parse(new File(args[0]));
}catch(ParserConfigurationException e1) {
}catch(SAXException e2) {
}catch(IOException e3) {
getAttributes();
public void getAttributes() {
// Retrive the entire Document from the Dom Tree
element = doc.getDocumentElement();
// System.out.println(element);
NamedNodeMap attrs = element.getAttributes();
// Get number of attributes in the element
int numAttrs = attrs.getLength();
// Process each attribute
for (int i=0; i<numAttrs; i++) {
Node node = attrs.item(i);
// Get attribute name and value
String attrName = node.getNodeName();
String attrValue = node.getNodeValue();
System.out.println(attrName + ": " + attrValue);
String s1 = element.getTagName();
System.out.println(s1);
// To get all the elements in a DOM Tree
NodeList nl1 = element.getElementsByTagName("*");
int i2 = nl1.getLength();
System.out.println(i2);
for(int i=0; i<i2; i++) {
System.out.println(nl1.item(i) + "\n");
} -
Search and Delete a specific record from a CSV file
Hi All,
I am new to java . I want to search for the records from CSV file and delete the row form the file.
Below is my Sample .csv
100||a100||1b100
200||b200||dc300
200||bg430||ef850
400||f344||ce888
Now I need some help in below requirements.
1.How to delete a record having value 200 and b200?
2.If record already exists how to update the existing record with new values?
Please share your ideas or give me some code snippet..
Thanks in AdvanceIn that case Do i need to write the entire contents of my file to a hash table(sumthng like this) and modify the Second row in my case with the new values..
is it possible??I would have done like this (though there maybe better methods)
1- create a class representing the record.
class Record{
String field1;
String field2;
String field3;
// and so on....
//setters
public void setFeild1(String str){
field1=str;
// and so on....
//getters
public String getFeild1(){
field1=str;
// and so on....
public String toString(){
return(field1+"||"+field2+"||"+field3);
}//end class2- then create an ArrayList meant to have objects of this class (Generics).
3- read from the file , create a new Record Object and add that to the ArrayList
4- perform operations on the ArrayList (you can add new records, and delete record, update......)
5- write the record back to file using 'toString()' method.
is there ne sample code available for thisdon't know, but you rarely get full code on forums.....outline given can be followed
Thanks!
Edit: It appears that 'r035198x' and me have the same point. This shows that this methodology is almost a standard way( if we ignore the Random access files.....)
Edited by: T.B.M on Jan 13, 2009 2:39 PM -
How to read specific lines from a text file using external table or any other method?
Hi,
I have a text file with delimited data, I have to pick only odd number rows and load into a table...
Ex:
row1: 1,2,2,3,3,34,4,4,4,5,5,5,,,5 ( have to load only this row)
row2: 8,9,878,78,657,575,7,5,,,7,7
Hope this is enough..
I am using Oracle 11.2.0 version...
ThanksThere are various ways to do this. I would be inclined to use SQL*Loader. That way you can load it from the client or the server and you can use a SQL*Loader sequence to preserve the row order in the text file. I would load the whole row as a varray into a staging table, then use the TABLE and MOD functions to load the individual numbers from only the odd rows. Please see the demonstration below.
SCOTT@orcl12c> HOST TYPE text_file.csv
1,2,2,3,3,34,4,4,4,5,5,5,,,5
8,9,878,78,657,575,7,5,,,7,7
101,201
102,202
SCOTT@orcl12c> HOST TYPE test.ctl
LOAD DATA
INFILE text_file.csv
INTO TABLE staging
FIELDS TERMINATED BY ','
TRAILING NULLCOLS
(whole_row VARRAY TERMINATED BY '/n' (x INTEGER EXTERNAL),
rn SEQUENCE)
SCOTT@orcl12c> CREATE TABLE staging
2 (rn NUMBER,
3 whole_row SYS.OdciNumberList)
4 /
Table created.
SCOTT@orcl12c> HOST SQLLDR scott/tiger CONTROL=test.ctl LOG=test.log
SQL*Loader: Release 12.1.0.1.0 - Production on Tue Aug 27 13:48:37 2013
Copyright (c) 1982, 2013, Oracle and/or its affiliates. All rights reserved.
Path used: Conventional
Commit point reached - logical record count 4
Table STAGING:
4 Rows successfully loaded.
Check the log file:
test.log
for more information about the load.
SCOTT@orcl12c> CREATE TABLE a_table
2 (rn NUMBER,
3 data NUMBER)
4 /
Table created.
SCOTT@orcl12c> INSERT INTO a_table (rn, data)
2 SELECT s.rn,
3 t.COLUMN_VALUE data
4 FROM staging s,
5 TABLE (s.whole_row) t
6 WHERE MOD (rn, 2) != 0
7 /
17 rows created.
SCOTT@orcl12c> SELECT * FROM a_table
2 /
RN DATA
1 1
1 2
1 2
1 3
1 3
1 34
1 4
1 4
1 4
1 5
1 5
1 5
1
1
1 5
3 101
3 201
17 rows selected. -
Read specified line from a text file
Hi,
I am trying to read a specific line from a text file. I don't want to read all of it but just this specific line... say line number 2. Is there a method built-in in java or should I code this myself?
Thanks and regards,
Krt_MaltaKrt_malta wrote:
I am trying to read a specific line from a text file. I don't want to read all of it but just this specific line... say line number 2. Is there a method built-in in java or should I code this myself?Is there anything in your use case that precludes using the offset of the start of the line rather than the line number? -
How do I pull up only one specific calendar at a time on my iPhone (i.e. 'home calendar')?
Thanks!In the calendar app, press the button in the upper left, "Calendars" and turn off/on the ones you want/don't want.
-
How can I enlarge the "all-day" screen on the iPhone calendar? I use the all-day screen for all my tasks, and it only shows 2 items at a time!! Help!!!
The iPhoto App can select all of the photos in the All Imported folder and delete them.
-
Is there a way to select a certain box of elements from a csv file and read that into LabVIEW?
Hello all, I was wondering if there was a way to select only a certain "box" of elements from a .csv file in LabVIEW? I have LabVIEW 2011 and my main goal is to take two arrays and graph them against each other. I can import the .csv file just fine and separate each row and each column to be its own, but say I have an 8X8 but want to graph the middle 4X5 or something like that. Is there any way to extract an array without starting at the beginning and without ending at the end? Thank you in advance.
Solved!
Go to Solution.Hi Szklanam,
as a CSV file is just a TXT file with a different suffix you can read a certain number of lines of that file. So you can limit the number of rows in your resultung array. To limit the number of columns you still have to use ArraySubset, so maybe it's a lot easier to read the full CSV file and pick the interesting spots with ArraySubset...
Best regards,
GerdW
CLAD, using 2009SP1 + LV2011SP1 + LV2014SP1 on WinXP+Win7+cRIO
Kudos are welcome -
How to read a specific line of a .dat file?
can we read a specific line of a .dat file, if so, how?
i have this .dat files for each customer, which contain their name on the first line, their id on the second line
and a list of items and an item id between each item.
John Doe
455689
milk
1
orange
9
Here is what I am trying to do, please correct me
//list out .dat file in dir
File customer_dir = new File ("Customer")
if (customer_dir.isDirectory())
String[] listofdat=dir.list();
for (int i=0;i<listofdat.length();i++)
Scanner dat_read = new Scanner (new File listfodat);
dat_read.nextLine();
if (dat_read.nextLine().equals("455689")) <--is this okay?bibiancheng wrote:
would you please explain to me what
String[] tokens = line.trim().split("\\s*,\\s*")"\\s*,\\s*" is a regex where \\s means whitespace and the * means "any number of".
so \\s* means that it will grab all the whitespace if there is any.
trim() removes white space from the beginning and end of the string.
dont put a comma in the name.
spaces allowed: "jane doe, 12-14-09, 428438, 6, 388473, 7, 187374, 3"
it was just a recommendation. breaking it into separate lines would be fine.
Maybe you are looking for
-
Close a first VI after switching to a second VI
OK. I have a request to develop a program that will basically call a first VI and then close it after opening a second VI. I haven't come across doing this before (usually there is a "main" VI at start-up that runs throughout the program). I am cur
-
Kernel/driver downward compatibility
Dear all, I have an EL5 Update 4 installation. My WLAN card has a BCM4318 chip from Broadcom. As stated on Broadcom's home page, I normally needed a b43 driver--however, this requires kernel 2.6.25 or higher. My kernel is 2.6.18, as originally downlo
-
List/Enumeration of Elements in a View (WD for Java)
Hello, How do I get a complete list of all elements in a view at runtime? I want to dynamically modify 30+ elements, but I don't want to have to code each of them. Is there a way to do this? Thanks, Martin
-
Message goes to 2 systems incorrectly
Hi, I have a scenario from SAP to Destination system. The problem is that I have 2 production systems, the other is a DRP and normal Prod. When I send messages from SAP to Prod, some manage to get into DRP. I am using the same Program ID(Created on s
-
Running Weblogic 8.1 SP3 and IPlanet 6.0 on same machine
I have requirement to run Weblogic Platform SP3 and IPlanet 6.0 servers on same Solaris machine for a client of ours. Please let me if we can do this. What kind of issues we may run into. Thanks, Sanjay