Autor Beitrag
FrankS
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18

Win XP Pro
D2006 Pro
BeitragVerfasst: Do 04.01.07 09:11 
Hallo Zusammen,

erst einmal allen ein frohes und gesundes Jahr 2007.

Ich habe ein kleines Problem beim einlesen einer csv Datei. Die Datei ist wie folgt aufgebaut :

DSI HEADER,32,2006-12-19,2006-12-18,2006-12-18,SELLOUT,040,Firma,NL,5821
LINE START,1,23631,P23631,,2,2372,425,0,0,0,0,N
LINE START,2,9135,V1410,,56,5574,3685,0,3000,0,13,N
LINE START,3,9132,V1412,,297,3723,1281,0,2000,0,9,N
LINE START,4,23630,P23630,,0,422,25,0,0,0,0,N
LINE START,5,20030,V1401,,931,2422,2422,0,7000,0,-20,N

Wie man sieht, ist hinter dem letzten Eintrag kein Komma mehr. Wie bekomme ich es hin, dass
mir mein Programm auch den letzten Eintrag einliest ?

Folgenden Code habe ich dafür bis jetzt geschrieben :

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
procedure TForm1.Button1Click(Sender: TObject);
var F : Textfile;
    SZ, pfad,Dateiname : String;
    sl: tStringList;
    i,durchlauf: integer;
    s, tmpheader, tmpref, tmpdruckdatum,tmpperstart, tmpperend, tmpbericht, tmpid, tmpname, tmpland, tmpidkd : string;
    SRecord, IRecord : TSearchRec;
    
Begin
 durchlauf := 0;
 pfad := '\\w2kserver\ftp\edibex\dsi\';

//Testen ob Dateien vorhanden sind
//FindFirst ist nur dann 0, wenn das Verzeichnis nicht leer ist
if FindFirst(pfad + '*.csv', faAnyFile, SRecord) = 0 then
begin
  while FindFirst(pfad + '*.csv', faAnyFile, SRecord) = 0 do begin
    durchlauf := durchlauf +1; //Hochzählen der eingelesenen Dateien
    sl := tStringlist.Create;
  try

    FindFirst(pfad + '*.csv', faAnyFile, IRecord);
    sl.LoadFromFile(pfad + IRecord.Name);

    tmpidkd := '';
    tmpland := '';
    tmpperstart := '';
    tmpperend := '';

    Dateiname := pfad + IRecord.Name;
    AssignFile(F,Dateiname);    //Datei öffnen
    Reset(F);                   //An den Anfang
    Readln(F,SZ);               //Erste Zeile der Datei nach SZ einlesen
    CloseFile(F);

      //Headerinformationen temporär einlesen
      tmpheader := copy(sz,1,pos(',',sz)-1);
      delete(sz,1,pos(',',sz));

      tmpref := copy(sz,1,pos(',',sz)-1);
      delete(sz,1,pos(',',sz));

      tmpdruckdatum := copy(sz,1,pos(',',sz)-1);
      delete(sz,1,pos(',',sz));

      tmpperstart := copy(sz,1,pos(',',sz)-1);
      delete(sz,1,pos(',',sz));

      tmpperend := copy(sz,1,pos(',',sz)-1);
      delete(sz,1,pos(',',sz));

      tmpbericht := copy(sz,1,pos(',',sz)-1);
      delete(sz,1,pos(',',sz));

      tmpid := copy(sz,1,pos(',',sz)-1);
      delete(sz,1,pos(',',sz));

      tmpname := copy(sz,1,pos(',',sz)-1);
      delete(sz,1,pos(',',sz));

      tmpland := copy(sz,1,pos(',',sz)-1);
      delete(sz,1,pos(',',sz));

      tmpidkd := copy(sz,1,pos('',sz)-1);
      //delete(sz,1,pos('',sz));



      //Anfang mit den Datenzeilen einlesen ab Zeile 2
      for i := 1 to sl.Count -1 do
      begin

        s := sl.Strings[i];

        Datamodule2.DSI_FILES.Active := True;
        Datamodule2.DSI_FILES.Insert; //Datenbank öffnen

        //Spalte 1
        Datamodule2.DSI_FILESHeader.Text := copy(s,1,pos(',',s)-1);
        delete(s,1,pos(',',s));

        //Spalte 2
        Datamodule2.DSI_FILESLineNumber.Text := copy(s,1,pos(',',s)-1);
        delete(s,1,pos(',',s));

        //Spalte 3
        Datamodule2.DSI_FILESVendorCode.Text := copy(s,1,pos(',',s)-1);
        delete(s,1,pos(',',s));

        //Spalte 4
        Datamodule2.DSI_FILESVikingCode.Text := copy(s,1,pos(',',s)-1);
        delete(s,1,pos(',',s));

        //Leerfeld
        copy(s,1,pos(',',s)-1);
        delete(s,1,pos(',',s));

        //Spalte 5
         Datamodule2.DSI_FILESSellOutQuantity.Text := copy(s,1,pos(',',s)-1);
        delete(s,1,pos(',',s));

         //Spalte 6
         Datamodule2.DSI_FILESOnHandQuantity.Text := copy(s,1,pos(',',s)-1);
         delete(s,1,pos(',',s));

         //Spalte 7
         Datamodule2.DSI_FILESSaftyStockQuantity.Text := copy(s,1,pos(',',s)-1);
         delete(s,1,pos(',',s));

         //Spalte 8
        Datamodule2.DSI_FILESTotalReceivedQuantity.Text := copy(s,1,pos(',',s)-1);
        delete(s,1,pos(',',s));

         //Spalte 9
         Datamodule2.DSI_FILESTotalOpenOrdersQuantity.Text := copy(s,1,pos(',',s)-1);
        delete(s,1,pos(',',s));

        //Spalte 10
         Datamodule2.DSI_FILESCostumerBackOrersQuantity.Text := copy(s,1,pos(',',s)-1);
         delete(s,1,pos(',',s));

         //Spalte 11
         Datamodule2.DSI_FILESMincellaneousQuantity.Text := copy(s,1,pos(',',s)-1);
         delete(s,1,pos(',',s));

         //Spalte12
         Datamodule2.DSI_FILESWatchProductFlag.Text := copy(s,1,pos(',',s)-1);
         delete(s,1,pos(',',s));

         Datamodule2.DSI_FILESBranchCode.Text := tmpidkd;
         Datamodule2.DSI_FILESland.Text := tmpland;
         Datamodule2.DSI_FILESPeriodstart.Text := tmpperstart;
         Datamodule2.DSI_FILESPeriodend.Text := tmpperend;

Vielen Dank schon mal im Voraus.

Gruß Frank

Moderiert von user profile iconjasocul: Delphi-Tags hinzugefügt
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Do 04.01.07 10:15 
Moin!

Probier mal den Code von hier; einfach die CSV-Datei mal damit einlesen und durchklicken, dann sollte dir klar werden, wie man das alternativ machen könnte. ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
tolosonic
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 36



BeitragVerfasst: Do 04.01.07 10:38 
sonst prüf doch direkt nach dem einlesen jeder zeile ob der string ein komma hinten dran hat und wenn nicht setzt du eins dran. ist zwar von hinten durch die ... aber so kannste deinen code so lassen, wie er ist. nich?
Miri
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 657


Delphi 3 Prof., Delphi 2005 PE
BeitragVerfasst: Do 04.01.07 10:50 
:gruebel:

Wenn ich das richtig verstehe, kopierst du immer alles bis zum nächsten Komma in deine Variablen und löschst es dann aus dem eingelesenen String...
Das bedeutet imho dass du bis einschließlich der vorletzten Spalte alles rauslöschst... ergo steht dann nur noch die letzte Spalte (mit oder ohne Komma, who cares) in dem eingelesenen String...
Du musst also gar nicht mehr nach nem Komma suchen...
ausblenden Delphi-Quelltext
1:
LetzteSpalte := s					


Oder steh ich jetzt total aufm Schlauch?!

_________________
Anonymes Eckenkind
FrankS Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18

Win XP Pro
D2006 Pro
BeitragVerfasst: Do 04.01.07 11:15 
Hallo Miri,

danke darauf hätte ich auch selber kommen können. Ich glaube mir hängtg Sylvester noch nach :oops:

Vielen Dank an alle
Frank