Fast .csv file reading

Z

Thread Starter

Zsolt

Dear All,
i have a problem. Our Scada system makes the arhive file from the database into a .csv file.
Have somebody a fast algoritm or example (Vb, Deplhi or C++)to read a csv file, Does somebody know how the Excel works with .csv file?
Thank You!

Best regards!
Zsolt
 
You need a parser. This parser needs to be aware of the different data types delimited by the comas (strings, ints, floats...). As it parses the data in the csv file, you need to process it in some way. Add it to an Access table? Calulate statistical data?
 
P

Paul K. P.E.

.CSV is native to Excel. All you have to do is double-click on the file and it should open immediately in Excel. If it doesn't, you might have to associate it in Windows to Excel.

Once in Excel, it is very easy to sort, chart, and analyze your data. Look up "Macros" in the Excel help file if you need to automatically do some data manipulation.

Paul
 
S
It's real simple. CSV stands for comma separated values.
It's simply a text file, with a format like:

value1.0,value2.0,value3.0
value1.1,value2.1,value3.1
...etc.
EOF
--
Steve Myres, PE
Automation Solutions
(480) 813-1145
 
M

Michael Griffin

"Reading" a CSV file is never a problem. The question is what are you trying to do with it? Are you trying to convert it to a different format, analyse data in it, decimate data, or something else?
 
B

Brian Boothe

<p>Provided by http://www.insoftdevelopment.com
<pre>
Private Sub makefile()
On Error Resume Next
Adodc1.Refresh
c = 0
Open app.Path & "\LISTEX.csv" For Append As #1
Write #1, c, Label1, Label2, Label3, Label4, Label5, Label6, Label7, Label8
Close #1

Do While Not Adodc1.Recordset.EOF
c = c + 1

Call textfile

Adodc1.Recordset.MoveNext
Loop
If Adodc1.Recordset.EOF = True Then Adodc1.Refresh
MsgBox "Your csv file is ready. CLICK 'OPTIONS / View csv File to see the
file you have created."
Frame1.Visible = False
End Sub


Private Sub textfile()
'allows you to make headers
Open app.Path & "\LISTEX.csv" For Append As #1

Write #1, c, Adodc1.Recordset.Fields(Label1.Caption),
Adodc1.Recordset.Fields(Label2.Caption),
Adodc1.Recordset.Fields(Label3.Caption),
Adodc1.Recordset.Fields(Label4.Caption),
Adodc1.Recordset.Fields(Label5.Caption),
Adodc1.Recordset.Fields(Label6.Caption),
Adodc1.Recordset.Fields(Label7.Caption),
Adodc1.Recordset.Fields(Label8.Caption)

Close #1

End Sub

-----</pre>
<p>This snip is perfect for CSV files that you want to open in Excel:
<pre>
strTmp As String
Open strPath For Output As #1
RecordsetToCSV = False

Print #1, """"
Do Until rs.EOF
' shoots blocks of 100 rows for speed
tmp = rs.GetString(, 100, """,""", """" & vbCrLf & """", "")
If rs.EOF Then
' drop the extra double quoted character sent
tmp = Left$(tmp, Len(tmp) - 1)
End If
Print #1, tmp;
Loop
RecordsetToCSV = True

End FunctionClose #1
</pre>
 
B

Brian Boothe

<p>Here in Delphi provided by http://www.insoftdevelopment.com
<pre>
procedure TForm1.FileLoadClick(Sender: TObject);
var
FileName1, sRecord: string;
Row: integer;
begin
// Let user select a file.
// Skip this until next lesson. For now, simply hard-code it.
FileName1 := 'C:\ParseCSV\Office.csv';

ListBox1.Items.LoadFromFile(FileName1); // 2.
StringGrid1.RowCount := ListBox1.Items.Count; // 3.

// for every record... ( count starts at 0 ! )
for Row := 0 to ListBox1.Items.Count - 1 do begin
sRecord := ListBox1.Items[Row];
ParseRecord(sRecord, Row); // 4.
end;

// 5. Select first "data" cell
StringGrid1.Row := 1;
StringGrid1.Col := 0;
StringGrid1.SetFocus;
end;
</pre>
 
But of you open this file directly in to excel scada will stop updating it I have tried this... you need some sort of quries to retrive that data from file first.
 
M

Martin Severgnini

Hi Zsolt.

We develop some programs to handle P-CIM csv files generated from .his files. Please contact me at [email protected] for more information.

Greetings,

Martin
Jagus Automation Inc.
 
M

Michael Griffin

This is likely because two different programs are each trying to gain exclusive access to the same file. This is likely because both programs are
asking for write privileges. The operating system is only allowing one program to get exclusive access. You need to work on a copy of the file.

Ideally, the program creating the file should close the file and start a new one after a defined interval (e.g. every hour). The program reading the file can then open any that are not currently being written to (not from the current time interval).
 
Hello,
As has been said upthread, what are you trying to do with it? As you can see, there's no point asking for help if we don't tell us that - you never said in the original query that you need to read it while the other program is still updating it, so nobody considered that (not very difficult) requirement. Instead, we waste time with suggestions that you have already tried and
rejected.

One suspects that if you write down what you need, with all the details, that the solution will become obvious. If it doesn't, at least you'll have something to show others when you ask for help.

Jiri
--
Jiri Baum <[email protected]> http://www.baum.com.au/~jiri
MAT LinuxPLC project --- http://mat.sf.net --- Machine Automation Tools
 
Top