Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > File I/O and Registry > Delete unwanted Columns and add some text to 1st column of a csv


Reply
 
Thread Tools Display Modes
  #1  
Old 07-24-2012, 10:00 PM
rohitb rohitb is offline
Freshman
 
Join Date: Jun 2006
Posts: 26
Default Delete unwanted Columns and add some text to 1st column of a csv


i have attached my csv. there are many columns like "Commodity" , "Ex-basis delivery centre", "Price unit" etc i don't want and i want to delete. how can this be done

also i want to add 1,2,3 such numbers as per expiry to the first column. Example
SYMBOL EXPIRY
BARLEYJPR 8/17/2012
BARLEYJPR 9/20/2012
BARLEYJPR 10/19/2012
BARLEYJPR 11/20/2012

in first line i would like to add 1 to BARLEYJPR as the expiry is on august 17th 2012(8/17/2012) which is nearest of all then would like to add 2 to BARLEYJPR as expiry is in September then would like to add 3 to BARLEYJPR as expiry is in October and lastly 4 to BARLEYJPR as expiry is in November
Attached Files
File Type: zip 24-07-2012.zip (1,014 Bytes, 3 views)
Reply With Quote
  #2  
Old 07-25-2012, 12:21 PM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,882
Default

Assuming you do not need to sort...

Read the csv (Comma Separated Values) as a text file as lines into an array of string.

Create a variable to hold the last lines (Symbol).
Create a variable to hold a group counter.

Begin Loop
Split each line of the array by using a comma as the delimiter.
Capture the first two columns of each line (Offset 0 and Offset 1).

if the current line symbol = the last line symbol then increase the counter by 1.
otherwise set the counter back to 1.

Add the counter to the current line symbol.
Overwrite the line with just the first two elements (Comma separated)
End Loop

Write the array of strings back to the file overwriting it.

Tools to use:
System.IO.File.ReadAllLines()
String.Split()
System.IO.File.WriteAllLines()
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #3  
Old 07-28-2012, 09:22 PM
rohitb rohitb is offline
Freshman
 
Join Date: Jun 2006
Posts: 26
Default

can u help me with some sample code
Reply With Quote
  #4  
Old 07-29-2012, 09:06 AM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,882
Default

Yes, but you have to make an effort first. You've been given a rough outline above. Try fleshing it out and post what you have done here. We will then give you pointers.

This is a teaching site. We do not write code to order.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #5  
Old 07-29-2012, 12:15 PM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
Default

I'm a bit confused, so you should clarify what version of Visual Basic you are using.
This thread is in the legacy VB side of the forum (VB6 and earlier), but Gruff is showing VB.Net method calls in the File I/O.
The File I/O Tutorial may be helpful if you're using VB6, but if you're using VB.Net (i.e. 2001 or later), then this thread will need to be moved to the VB.Net side of the forum.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #6  
Old 07-30-2012, 01:45 PM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,882
Default

Pretty much the same as in VB.NET except you would probably loop through the file instead of reading the whole thing in one go.

Untested Code
Code:
Option Explicit Private Sub Command1_Click() Dim sLineIn as string Dim sLineOut as string Dim sCols() as string Dim sSymbol as string Dim sDate as string Dim sLastSymbol as string Dim nCounter as integer sLastSymbol = "nnn" 'Set to something Symbol cannot possibly be initially. nCouner = 0 Open <Your output file name goes here> for output as #2 Open <Your input file name goes here> for input As #1 Do while not eof(1) Line input #1, sLineIn sCols = Split(sLineIn, ",") sSymbol = sCols(0) sDate = sCols(1) if sSmbol = sLastSymbol then nCounter = nCounter + 1 Else nCounter = 1 End If sLastSymbol = sSymbol sLineOut = sSymbol & "_" & nCounter & "," & sDate Print #2, sLineOut & vbcrlf Loop Close #1 Close #2 ' Delete file 1 and rename file 2 as the original file 1 name. End Sub
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #7  
Old 07-31-2012, 04:01 AM
rohitb rohitb is offline
Freshman
 
Join Date: Jun 2006
Posts: 26
Default

below is the attached file which contains the code i wrote to read the csv file to array. it also has a csv file which i want to read

can some one modify it so it outputs the text to another csv file with below columns
Symbol,Expiry Date,Opening Price,High Price,Low Price,Closing Price,Quantity Traded Today,Open Interest In Quantity,LastTradeDate

rest columns i want to delete
Attached Files
File Type: zip csv_to_array.zip (6.1 KB, 1 views)
Reply With Quote
  #8  
Old 07-31-2012, 04:51 PM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,882
Default

If you look at my post above you can see that "Cols()" contains all the column data per row.

If Cols(0) = Symbol and Cols(1) = Expiry Date
the what do you suppose Cols(2)...Cols(n) contains?

Gather the column information in the sLineOut variable separating them with commas,

The example I showed above also shows how to write out to a second file.

What part of the solution do you not get?
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #9  
Old 08-01-2012, 02:02 AM
rohitb rohitb is offline
Freshman
 
Join Date: Jun 2006
Posts: 26
Default

Quote:
Originally Posted by Gruff View Post
If you look at my post above you can see that "Cols()" contains all the column data per row.

If Cols(0) = Symbol and Cols(1) = Expiry Date
the what do you suppose Cols(2)...Cols(n) contains?

Gather the column information in the sLineOut variable separating them with commas,

The example I showed above also shows how to write out to a second file.

What part of the solution do you not get?
sorry i missed it... ur solution is great n its 99.99% done

one more thing .. in some files value in column QUANTITY TRADED TODAY is zero . how to eliminate that line
Reply With Quote
  #10  
Old 08-01-2012, 02:11 PM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,882
Default

"0" or empty?

So say Column 12 is the Column in question...

Code:
If Cols(12) <> "0" then ' Only write lines that do not have "0" in Cols(12) Print #2, sLineOut & vbcrlf End if
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 08-01-2012 at 02:18 PM.
Reply With Quote
  #11  
Old 08-02-2012, 06:39 AM
rohitb rohitb is offline
Freshman
 
Join Date: Jun 2006
Posts: 26
Default

Quote:
Originally Posted by gruff View Post
"0" or empty?

So say column 12 is the column in question...

Code:
if cols(12) <> "0" then ' only write lines that do not have "0" in cols(12) print #2, slineout & vbcrlf end if
thanks gruff bro u where great help
Reply With Quote
  #12  
Old 08-03-2012, 09:10 PM
rohitb rohitb is offline
Freshman
 
Join Date: Jun 2006
Posts: 26
Default

Quote:
Originally Posted by Gruff View Post
"0" or empty?

So say Column 12 is the Column in question...

Code:
If Cols(12) <> "0" then ' Only write lines that do not have "0" in Cols(12) Print #2, sLineOut & vbcrlf End if
Code:
Private Declare Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" _
(ByVal pCaller As Long, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long

Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
Alias "DeleteUrlCacheEntryA" _
(ByVal lpszUrlName As String) As Long

Private Const ERROR_SUCCESS As Long = 0
Private Const BINDF_GETNEWESTVERSION As Long = &H10
Private Const INTERNET_FLAG_RELOAD As Long = &H80000000

Private Sub Command1_Click()
On Error GoTo Err
Const q As String = "-"
Dim tmp As String, fName As String, Pos As Long, fPath As String
Dim first As Date, last As Date, spath As String, d As Date
Dim sLineIn As String
Dim sLineOut As String
Dim sCols() As String
Dim sSymbol As String
Dim sName As String
Dim sDate As String
Dim sOpen As String
Dim sHigh As String
Dim sLow As String
Dim sClose As String
Dim sVolume As String
Dim sOpenIntrest As String
Dim sLastSymbol As String
Dim nCounter As String

cap = Me.Caption
If Dir(App.Path & "\NCDEX\", vbDirectory) = "" Then
MkDir App.Path & "\NCDEX\"
End If
spath = App.Path & "\NCDEX\" ' folder to save files : note trailing \
first = MonthView1
last = MonthView2
strURL = "http://www.ncdex.com/Downloads/Bhavcopy_Summary_File/Export_csv/"
For d = first To last
sSourceURL = strURL & Format(d, "MM") & q & Format(d, "dd") & q & Format(d, "yyyy") & ".csv"
Debug.Print sSourceURL
fName = Format(d, "dd-mm-yyyy") & ".csv"
slocalfile = spath & fName
Me.Caption = "Downloading " & fName
Call DeleteUrlCacheEntry(sSourceURL)
URLDownloadToFile 0&, sSourceURL, slocalfile, BINDF_GETNEWESTVERSION, 0&

'  sLastSymbol = "zzz"  'Set to something Symbol cannot possibly be initially.
nCounter = 0
Open App.Path & "\temp.csv" For Output As #2
Open App.Path & "\NCDEX\" & fName For Input As #1
Do While Not EOF(1)
Line Input #1, sLineIn
sLineIn = Replace(sLineIn, Chr(39), vbNullString)
      sLineIn = Replace(sLineIn, Chr(34), vbNullString)
      sLineIn = Replace(sLineIn, " ", vbNullString)
      sCols = Split(sLineIn, ",")
      sSymbol = sCols(0)
      sDate = sCols(15)
      sOpen = sCols(6)
      sHigh = sCols(7)
      sLow = sCols(8)
      sClose = sCols(9)
      sVolume = sCols(10)
      sOpenIntrest = sCols(14)
      
      If sSymbol = sLastSymbol Then
        nCounter = nCounter + 1
      Else
        nCounter = 1
      End If
      sLastSymbol = sSymbol
      
      Debug.Print sLineIn
      If sCols(10) <> "0" Then
  ' Only write lines that do not have "0" in Cols(12)
  
      sLineOut = sSymbol & "_" & nCounter & "," & sDate & "," & sOpen & "," & sHigh & "," & sLow & "," & sClose & "," & sVolume & "," & sOpenIntrest
      Print #2, sLineOut
      End If
    Loop
  Close #1
  Close #2

  ' Delete file 1 and rename file 2 as the original file 1 name.
  ' // Delete the original file
    Kill slocalfile
  ' // Rename the temp file to the original name
    Name App.Path & "\temp.csv" As slocalfile

Next
Me.Caption = cap
'YOU CAN TAKE THIS BELOW OUT IF U DONT WANT IT
MsgBox "Saved to " & spath, vbInformation + vbOKOnly, "Success!"
Exit Sub
Err: Open App.Path & "\log.log" For Append As #1
Print #1, Err.Number & " - " & Error$
Close #1
End Sub
this is the final code ..above code downloads csv files from server and cleans up the files and outputs the necessary data to another csv file.

my issue is on sunday and holidays there is no file created on server. so when such date is encountered in the SELECTION OF FROM AND TO DATE the program stops going further . how can this be avoided

pls help me i want the program to go to next date if there is no file on previous date
Reply With Quote
  #13  
Old 08-07-2012, 10:03 AM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,882
Default

Your being somewhat unclear again. On which line of code does your program hang?

In general though... If you know something to always be true then you can write code to exploit that condition. Another approach is to check for an error where you know one would occur and handle it.

So If you know something will not be available for certain days then use an "If ... End If" Program flow statement to bypass the condition. In this case you would need some sort of function that takes a date as a parameter and returns True if Sunday and Holiday is not true

Use this in an if statement like you did before except bypass the entire process if true.
Lets call the function 'isProcessDay'

Code:
Private Sub Command_Click() 'Move all your existing code to a sub routine called GetRemoteData() If isProcessDay(Date) then GetRemoteData End If End Sub

As I do not know what constitues a Holiday for you I would suggest you google around for the topic "VB6 Function to Find a Holiday
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->