Trying to omit first line of CSV file (the header)
Hello everybody,
Newbie here havin a lil problem. I have this program below that reads in a CSV file. My test CSV file has the follow structure:
ID, LastName, FirstName, Phone, Email,
2345, Yez, Wei do, 456-789-0123, [email protected],
2744, Avanto, Aldo, 562-593-6500, [email protected],
1212, Dewy, Cheatem, 123-456-7890, [email protected],
1234, No, Wei Foo, 123-456-7890, [email protected],
The first line is my header. (here is the heart of my problem)
I have the program reading in the file and the it sorts it. The problem i have is that the first line, my header gets sorted with everything else. Here is my feable attempt with resulting error:
import java.io.*;
import java.util.*;
public class File2Array {
public static void main(String[] args)
File file = new File("person_test1.csv"); //File to read in
ArrayList persons = (ArrayList) getPersons(file); //call to arraylist
Collections.sort(persons);
for (Iterator i = persons.iterator(); i.hasNext(); )
System.out.println(i.next());
public static List getPersons(File file)
//NEW PIECE OF CODE boolean first = true;
ArrayList persons = new ArrayList();
try {
BufferedReader buf = new BufferedReader(new FileReader(file));
while (buf.ready()) {
String line = buf.readLine();
//NEW PIECE OF CODE
while( ( line = buf.readLine() ) != null )
if( first )
first = false; continue;
//ORIGINAL IF
// if (!line.equals("") )
//NEW IF
if (line != null && !line.equals("") )
//END OF NEW CODE
StringTokenizer tk = new StringTokenizer(line, ",");
ArrayList params = new ArrayList();
while (tk.hasMoreElements())
params.add(tk.nextToken());
Person p = new Person(
(String) params.get(0),
(String) params.get(1),
(String) params.get(2),
(String) params.get(3),
(String) params.get(4));
persons.add(p);
} catch (IOException ioe)
System.err.println(ioe);
return persons;
class Person implements Comparable
private String ID;
private String LastName;
private String FirstName;
private String Phone;
private String Email;
public Person(
String ID,
String LastName,
String FirstName,
String Phone,
String Email)
if (FirstName==null || LastName==null)
throw new NullPointerException();
this.ID = ID;
this.LastName = LastName;
this.FirstName = FirstName;
this.Phone = Phone;
this.Email = Email;
public String toString()
StringBuffer sb = new StringBuffer ();
sb.append( ID );
sb.append(" ");
sb.append( LastName );
sb.append(" ");
sb.append( FirstName );
sb.append(" ");
sb.append( Phone );
sb.append(" ");
sb.append( Email );
sb.append(" ");
return sb.toString();
public boolean equals(Object o)
if (!(o instanceof Person))
return false;
Person p = (Person)o;
return p.FirstName.equals(FirstName) &&
p.LastName.equals(LastName);
public int hashCode()
return 31*FirstName.hashCode() + LastName.hashCode();
public int compareTo(Object o)
Person p = (Person)o;
int lastCmp = LastName.compareTo(p.LastName);
return (lastCmp!=0 ? lastCmp :
FirstName.compareTo(p.FirstName));
this code compiles and runs but does not produce any output and I don't understand why.
Question: What is the proper way to have the code omit the first line of a CSV file??
By the way I do have another file called person.java that goes with this but I didn't think I had to post it.
Also am I posting correctly? Am I putting in too much code??
Than you!!
Hot diggity! jverd,
I got it to work!! here's what I did all thanks to you :-)
ArrayList persons = new ArrayList();
try {
BufferedReader buf = new BufferedReader(new FileReader(file));
//read through file and populate array
//while (buf.ready()) {
String firstline = buf.readLine(); //gets header, renamed line to firstline
String line; //added this line
System.out.println(firstline); //added this to print out firstline
//try this
while( ( line = buf.readLine() ) != null )
/*{ //commented out this portion
if( first )
first = false; continue;
//end try this
//if (line != null && !line.equals("") ) //down to here
if (!line.equals("") ) //added this
StringTokenizer tk = new StringTokenizer(line, ",");
ArrayList params = new ArrayList();
while (tk.hasMoreElements())
params.add(tk.nextToken());
Person p = new Person(
(String) params.get(0),
(String) params.get(1),
(String) params.get(2),
(String) params.get(3),
(String) params.get(4));
persons.add(p);
}
Similar Messages
-
Applescript to remove first line from CSV
Hi,
I receive CSV files that I then drag and drop into a Numbers spreadsheet.
The CSV files have two lines of data (which correspond to two rows of data in a Numbers spreadsheet).
The first line in each CSV is a header that I would like to delete before it goes into the spreadsheet.
I would like to write an Applescript (application) that I could drop the CSV onto, that would delete the first (header) line.
Seems like this should be simple, but I've spent the past hour searching for how to do this and I haven't found a clear answer anywhere.
If anybody could help I'd really appreciate it.
thanks.Saved as an Applescript App, this will delete the first line of text files dropped on the App
USE WITH CAUTION and Test
on open the_Files
repeat with i in the_Files
tell application "Finder"
if name extension of i is "csv" then
do shell script "sed -i \"\" '1 d' " & quoted form of POSIX path of i
end if
end tell
end repeat
end open -
Hi Experts,
This is File to IDOC scenario. The source file is a csv file with header and item records.
The file comes with column headings and now problem is how to remove the column headings using my content conversion.
Any help would be appericiated
Regards,
VijayHi Prakash,
Yes you are right but now my problem is the file will not have the header all the time. Actually the source files are generated from different legacy systems so some may have column headings and some may not have.. but the structure remains the same.
Regards,
Vijay
Message was edited by: Vijay Kuruvila
Message was edited by: Vijay Kuruvila -
When trying to recover a lost iTunes library file, the programme moved all the normal contents of iTunes folder to the desktop. Now what must I do? There is no iTunes Library.itl file. On the desktop it is labelled iTunes Library.xml. Several others files have appeared there too including iTunes Music Library.xml
Before I can use the programme is it necessary to take another step?
Thanks for advice.Don't worry. I'm all for caution. I guess I should have asked a little more about what steps you took while trying to recover your lost library?
The permissions thing may be a red herring, but if there is a problem with file access rights to the designated media folder iTunes has been known to place files on the desktop instead since it can access this location. The most common cause for such problems is working with a folder on an NTFS drive via a differnt account or on a different computer. In the normal course of events folders created inside user profiles have limited rights for other users.
Here are the typical layouts for the iTunes folders:
Normally the iTunes folder will be created at <User's Music>\iTunes with the media folder inside it. The library files should all appear in the same place so your comment about there not being an iTunes Library.itl file is confusing. Is it just the case that the .itl extension is hidden? The two .xml files makes sense from the above and indicates that the active library is not in <User's Music>\iTunes, but you have a recovered file from when it was.
If, in fact, the situation is that your desktop is acting as the main iTunes folder, and contains the library file, Album Artwork, iTunes Media and (optionally) Previous iTunes Libraries folders, with all other content being inside the iTunes Media folder, then it should be relativly easy to rearrange your into the correct shape and move them to the correct location.
If things are more complex, with something other than a library folder containing a media folder, then reestablishing the normal order is more complicated. The iTunes consolidate function is used to bring copies of files stored outside of the media folder into the media folder. It may sometimes be needed as part of the process of tidying up, however it leaves behind originals. There are circumstances where there isn't room for two complete sets of the media files on the same drive which is why I suggested the script might be useful as it produces the same results for the library without leaving the originals for manual clear up.
I think before you do anything however, we should get a clearer picture of exactly what is where. Could you perhaps take a screenshot of the desktop and post it here? Take a look at this post on making a split library portable. The same techniques I outline there can be used to lick your library into shape, it is just a question of working out which to use in which order for the best effect.
tt2 -
Hi, when typing an email the font on the first line is different then the second, can you help fix this
I can offer suggestions, but I can not fix it.
Press enter three or four times before you start typing and then move the cursor back to the start. What happens is your point of insertion gets outside your predefined font range, it is a know bug and that will help you "stay in the lines" Another option is type Rick, go back to the start and then type your message. Anything which keeps the insertion point from the very end of the email really. -
Only about 2/3 of the column show. Also, paragraphs on sidebars, such as the Youtube subtitle sentences are cut so after the first line I only see the top of the second line.
If you have increased the minimum font size then try the default setting "none" as a high value can cause issues like you described.
* Firefox > Preferences > Content : Fonts & Colors > Advanced > Minimum Font Size (none)
* Firefox > Preferences > Content : Fonts & Colors > Advanced > [X] "Allow pages to choose their own fonts, instead of my selections above"
Reset the page zoom on pages that cause problems: <b>View > Zoom > Reset</b> (Ctrl+0 (zero); Cmd+0 on Mac)
* http://kb.mozillazine.org/Zoom_text_of_web_pages -
Removing first line in text file... Memory and HD concern
Hello all,
I want to remove the first line of a text file. This is easy... One way
to do it, is to find the first "end of line" caracter, strip the string
and re-write the sting in file with the "strip to sreadsheet" VI.
Since the file can be quite large (about 120000 lines), is this a good
way to do it. Will it re-write all the line or just moving the "begin
of file" pointer to the second line? If it re-write the whole file,
this option is not very good since I could have to do this operation
quite often. (I want the file to be a kind of circular buffer / backup).
What do you think is the best way to do this?
Thanks!
VincentI think you would have to read in all the data and write it to a new file, you could either keep the data in RAM or simply write to another file then delete the original, and then rename the temp one. So obviously that is a lot of data and memory.
My main question is are you trying to use a text file as a circular buffer? If so can you tell us more about the data, ie is it strings of a constant length? Does a user ever need to read the file or could we head binary?
A solution would be to truly make the file a binary file, so you could actually seek through the file easier, and maybe make the first data chunk the location of the most recent write. The obvious danger is then writing over existing data. This solution is still risky because of the data writes.
You may want to look at some of the other file formats like HWS which maintains a hierarchy of data for you so you can simply add and remove data from it. However, a user will not be able to easily read it. -
The first line in a file is supressed
hi all,
i have a really weird problem.
i have a file with a lot of data and i am using a file adapter with file content conversion to parse it.
the file adapter is a sender one.
the problem come when i do read the file and then it ignore the first line of the file.
all the lines from the second one forward is shown in SXMB_MONI but the first line of the file isn't.
i can't seem to figure out what the problem is, i tryed looking for some kind of attribute in the file adpater but had no luck.
could you please help me?
btw,
the file structure is: header,1,body1,<wildcard>,body2,<wildcard>
Edited by: Roi Grossfeld on Oct 29, 2008 4:24 PMRecordset Structure: H1,1,B1,<wildcard>,B2,<wildcard>
Key Field Name: RecordType
H1.fieldFixedLengths 6,2,8,1,1,3,30,5
H1.fieldNames FileNo,RecordType,TransmissionDate,Car_Truck,BU,MD,FileName,Count
H1.keyFieldValue H1
B1.fieldFixedLengths 6,2,10,27,8,8,4,5,3,11,9,9,11,11,9,9,7,9,11
B1.fieldNames FileNo,RecordType,InvoiceNumber,VesselName,MMSEShipmentDate,PlanArrivalDate,DeliveryPlace,Units,Currency,ModelPriceAmount,OptionPriceAmount,ColourPriceAmount,REPriceAmount,TotalPriceAmount,FreightChargeAmount,InsuranceFeeAmount,MiscellaneousAmount,VATAmount,GrandTotalAmount
B1.keyFieldValue B1
B2.fieldFixedLengths 6,2,10,12,12,12,5,6,12,3,3,3,4,17,7,7,12,8,5,3,9,7,7,9,9,7,9,4,20
B2.fieldNames FileNo,RecordType,InvoiceNumber,MDOrderNumber,MMSEOrderNumber,MMSECaseNumber,ModelYear,BodyType,Model,ExteriorColourCode,InteriorTrimColourCode,OptionCode,RearEquipment,VehicleIdentificationNumber,ChassisModel,CSequenceNumber,EngineModel,ESequenceNumber,KeyNumber,Currency,UnitModelPrice,UnitOptionPrice,UnitColourPrice,UnitRearEquipmentPrice,UnitTotalPrice,UnitVATAmount,UnitTotalVAT,VATRate,VATNumber
B2.keyFieldValue B2
ignoreRecordsetName true
Edited by: Roi Grossfeld on Oct 29, 2008 4:39 PM -
File adapter SyncRead Operation adds "???" to first record of csv file
Hi All,
One interface is reading one CSV file (containing two fields only) using SyncRead Operation of File Adapter. But I found, when file adapter parsed the data it appends "???" to first field of the first record which cause further data validation in the process.
Test.csv file
123456,159357
147258,987654
Trace of the FileAdapter:
<messages>
<Invoke_FILE_Inbound_InputVariable>
<part name="Empty">
<empty/>
</part>
</Invoke_FILE_Inbound_InputVariable>
<Invoke_FILE_Inbound_OutputVariable>
<part name="body">
<Root-Element>
<TestElements>
<Emplid>���123456</Emplid>
<SupId>159357</SupId>
</TestElements>
<HRRespIDElements>
<Emplid>147258</Emplid>
<SupId>987654</SupId>
</TestElements>
</Root-Element>
</part>
</Invoke_FILE_Inbound_OutputVariable>
</messages>
Can anyone help me to understand , what is reason? Or I missed anything.
Thanks & Regards,
SharmisthaUnfortunately, this is documented bahaviour (solution/workaround anyone?!):
Elapsed Time Exceeds:
Specify a time which, when exceeded, causes a new outgoing file to be created.
Note:
The Elapsed Time Exceeds batching criteria is evaluated and a new outgoing file is created, only when an invocation happens.
For example, if you specify that elapsed time exceeds 15 seconds, then the first message that is received is not written out, even after 15 seconds, as batching conditions are not valid. If a second message is received, then batching conditions become valid for the first one, and an output file is created when the elapsed time exceeds 15 seconds. -
How to remove # from the end of each lines of .csv file.
Hi ,
I have uploaded .csv file from my local m/c (windows) to application server (unix) thru. FM 'ARCHIVFILE_CLIENT_TO_SERVER'
Issue is when I open the file in AL11 it is showing # at end of each line. Click below link to see the output file.
[https://docs0.google.com/document/edit?id=1PzjhljdCC2Wgj9L1dZ51G4pHJ0G_7jJwQAMqcJHdggc&hl=en#]
Pls. help me how to remove all the # from the file.
Thanks in advance.
DevinderHi ,
use this
Declare this
that # value is actually use of Tab in file
so in order to remove that use below code
DATA :C_TAB(1) TYPE c VALUE cl_abap_char_utilities=>HORIZONTAL_TAB .
loop at it_data .
refno = it_data-line .
REPLACE ALL OCCURRENCES OF c_tab IN refno WITH ''.
modify it_data .
endloop.
Regards
Deepak. -
Need help with java File IO ( Removing the first line from a file )
Hi guys ,
I am currently doing a project in which I need to extract out the values from the second line of a file to the end. The question is how do I ignore the first line ??
I thought of two possible answers myself. One is to use randomaccessfile to read and rewrite. But the file may be HUGE so storing the whole file in memory is not a very good idea.
Second is to jump to second line before doing while ((str = in.readLine()) != EOL) ... or just delete the first line from the file. Can anyone suggest a better solution or show me some sample codes ? Thanks.
regards
billyamJust skip the first line (bufferedReader.readLine()), add a comment, and then handle the rest of your file
-
How to delete contents of a csv file except header using powershell
Hi,
I am trying to delete all the content of my csv file except its header. currently I am using clear-content but using this headers are also getting deleted. Basically I need a command which deletes all the rows of a csv file except first row.Please help.I'm only going to respond to prove that the useless troll is useless and wrong.
Perhaps someday the troll will finally realize that it is worthless and will go away. No one cares about what it has to say and no one has ever found it to be helpful. Even good information is bad information when it is presented in an insulting, blathering,
incoherent, and belittling tone. Plus, I think it's really funny when something tries to justify its own sad existence by making itself feel superior (when it clearly isn't). Sometimes I almost feel bad for it, but then it reminds me of how big of a jerk it
really is.
Get a life, get some friends, just go do something that isn't totally worthless (yeah, I know, you can't..).
Don't retire TechNet! -
(Don't give up yet - 12,700+ strong and growing) -
First line indent, except for the very first paragraph
Hello,
In my paragraph style, I have First Line Indent selected. However; I'd like to leave the very first paragraph intact (without indent). Is there a way to do so?So simple! Should've thought about it.
Thanks. -
Pages printing in draft mode. First line normal and the rest is faint.
Please, how do I reset this.
Thanks
BruceIf you are using a printer (e.g. HP) that has an Economode (powersaver) setting, leave this off. Otherwise, your prints will look washed out as though toner was depleted.
Since Pages itself does not have a selectable “draft” mode, the focus will be on your printer hardware and configuration. -
How to create two headers line in CSV file for File Receiver Adpater ?
Hi friends ,
My scenario is RFC to File. I am downloading file in CSV format . May I know how do i add two line of staic headers Like follows
<b> CLOCTYP CFLOCTYP
Exe Loc. Type Cus Loc. Type</b>
Best Regards.,
V.RangarajanMy Input xml will be like
<?xml version="1.0" encoding="UTF-8"?>
<ns0:FileRec_MT xmlns:ns0="urn:tabFile">
<ContactFile>
<Contact>
<Name>Raghavendra</Name>
<LName>GURU</LName>
<Company>xxx</Company>
</Contact>
</ContactFile>
</ns0:FileRec_MT>
<b>Output in csv as</b>
Raghavendra, Guru ,xxx
My requirement is [ 2 header Lines ]
<b>CLOCTYP CFLOCTYP CFPLACE
Exe Loc.Type Cus Loc. Type Place</b>
Raghavendra Guru xxx
If multiple rows comes it has to come like
<b> CLOCTYP CFLOCTYP CFPLACE
Exe Loc. Type Cus Loc. Type Place</b>
Raghavendra Guru xxx
test test yyyyy
Maybe you are looking for
-
Recover RAID 5 array with new root drive
Ok, I have a root mirror and a 4 drive RAID 5 array on an E250. The linker got hosed on the root partition and mount fails as a result. I've tried repairing the linker to no avail. So I replaced c0t0d0 with a brand new drive and put a fresh install o
-
Adobe flash player update service 11,6 r602
Hello im having problem getting rid of box saying {adobe flash player update service 11.6 r602 stopped working and was closed} it also says { a problem caused the application to stop working correctly windows will nitify you if a solution is availabl
-
Old AEBS(g) into new network
Hi, I've recently replaced my wireless network in the house, to a Airport Extreme (n) connected to the internet and a Time Capsule (n) which extends the network. These two items have replaced my old Airport Extreme Base Station (white domed version,
-
Atist name in playlist with a local phone number?
Hi. I was syncing my iPhone 4s when I noticed a artist name on my play lists with a phone number from the city I live in. is this some type of adware?. I clicked on it and my computer got frozen, had to force shut down. any comments to this isue?.
-
how can i get itunes to quit skipping to the currently playing song when i'm working on the library somewhere else?