Grupo
Tengo la necesidad de leer un archivo txt que está delimitado por tabuladores, nunca he leído un archivo txt con hmg, ¿alguien tiene alguna idea o rutina o si me dan un poco de luz?
De antemano gracias por la ayuda
Leer archivo txt delimitado por tabulares
Moderator: Rathinagiri
-
jorge.posadas
- Posts: 192
- Joined: Mon May 19, 2014 7:43 pm
- DBs Used: DBF, SQLite, MS-SQL, ACCESS, MariaDB (en proceso)
- Location: Morelia, Mich. México
- Contact:
Leer archivo txt delimitado por tabulares
Cordialmente
POSADAS SOFTWARE
Jorge Posadas Ch.
Programador independiente
Morelia, Mich.
M é x i c o .
Movil +52 44 3734 1858
SKYPE: jorge.posadasch
Email: jorge.posadas@posoft.mx
POSADAS SOFTWARE
Jorge Posadas Ch.
Programador independiente
Morelia, Mich.
M é x i c o .
Movil +52 44 3734 1858
SKYPE: jorge.posadasch
Email: jorge.posadas@posoft.mx
-
Red2
- Posts: 281
- Joined: Sat May 18, 2019 2:11 pm
- DBs Used: Visual FoxPro, FoxPro
- Location: United States of America
Re: Leer archivo txt delimitado por tabulares
Hi Jorge,
I no longer have access to such code written some years ago in VFP.
Actually, you can read a text file with MEMOREAD() if the file is less than 64K
-OR- if it is greater than 64K use FOPEN(), FREAD(), FCLOSE() into a text string.
It is then pretty basic to check each character in a FOR...NEXT or DO WHILE loop.
When you find a CHR(9) it is a tab (column/field) delimiter.
A CHR(13)+CHR(10) (or such) indicates end of line (record delimiter).
I hope that this helps. I am away from home now but by mid-week should be back.
If you have any further questions I'd be able to help more them.
Best Regards, Red2
I no longer have access to such code written some years ago in VFP.
Actually, you can read a text file with MEMOREAD() if the file is less than 64K
-OR- if it is greater than 64K use FOPEN(), FREAD(), FCLOSE() into a text string.
It is then pretty basic to check each character in a FOR...NEXT or DO WHILE loop.
When you find a CHR(9) it is a tab (column/field) delimiter.
A CHR(13)+CHR(10) (or such) indicates end of line (record delimiter).
I hope that this helps. I am away from home now but by mid-week should be back.
If you have any further questions I'd be able to help more them.
Best Regards, Red2
-
Red2
- Posts: 281
- Joined: Sat May 18, 2019 2:11 pm
- DBs Used: Visual FoxPro, FoxPro
- Location: United States of America
Re: Leer archivo txt delimitado por tabulares
Hi Jorge,
I no longer have access to such code written some years ago in VFP.
Actually, you can read a text file with MEMOREAD() if the file is less than 64K
-OR- if it is greater than 64K use FOPEN(), FREAD(), FCLOSE() into a text string.
It is then pretty basic to check each character in a FOR...NEXT or DO WHILE loop.
When you find a CHR(9) it is a tab (column/field) delimiter.
A CHR(13)+CHR(10) (or such) indicates end of line (record delimiter).
I hope that this helps. I am away from home now but by mid-week should be back.
If you have any further questions I'd be able to help more them.
Best Regards, Red2
I no longer have access to such code written some years ago in VFP.
Actually, you can read a text file with MEMOREAD() if the file is less than 64K
-OR- if it is greater than 64K use FOPEN(), FREAD(), FCLOSE() into a text string.
It is then pretty basic to check each character in a FOR...NEXT or DO WHILE loop.
When you find a CHR(9) it is a tab (column/field) delimiter.
A CHR(13)+CHR(10) (or such) indicates end of line (record delimiter).
I hope that this helps. I am away from home now but by mid-week should be back.
If you have any further questions I'd be able to help more them.
Best Regards, Red2
Re: Leer archivo txt delimitado por tabulares
Hi jorge,
it is very simple.
You can create a table with one field to start with. Make it character with length a bit longer then the text file character length.
then you:
use table
append from textfile.txt delimited
browse ?
Your table should have the contents of the text file.
You can also create the table with a field for each item in text file (item meaning item before each in line) Like name, phone, and so on.
Then:
append from text.txt delimited with ','
If my description is confusing , if I can find a text file I could create a example for you.
Give me a short example of you text file and what you want to do with it.
Franco
it is very simple.
You can create a table with one field to start with. Make it character with length a bit longer then the text file character length.
then you:
use table
append from textfile.txt delimited
browse ?
Your table should have the contents of the text file.
You can also create the table with a field for each item in text file (item meaning item before each in line) Like name, phone, and so on.
Then:
append from text.txt delimited with ','
If my description is confusing , if I can find a text file I could create a example for you.
Give me a short example of you text file and what you want to do with it.
Franco
All The Best,
Franco
Canada
Franco
Canada
- AUGE_OHR
- Posts: 2096
- Joined: Sun Aug 25, 2019 3:12 pm
- DBs Used: DBF, PostgreSQL, MySQL, SQLite
- Location: Hamburg, Germany
Re: Leer archivo txt delimitado por tabulares
hi,
before you can "import" Data from *.CSV you must create DBF with "matching" FIELD(s)
open *.CSV using Editor (Notepad.EXE) and have a look how data "Type" look like
when 1st Line is a "Header" you must "skip" 1st Line
---
you can to use MEMOREAD(), MLCOUNT() and MEMOLINE() to "extract" a Line of *.CSV
when have hole line i use hb_ATokens() to get a Array of each Value
https://harbour.github.io/doc/harbour.html
before you can "import" Data from *.CSV you must create DBF with "matching" FIELD(s)
open *.CSV using Editor (Notepad.EXE) and have a look how data "Type" look like
when 1st Line is a "Header" you must "skip" 1st Line
---
you can to use MEMOREAD(), MLCOUNT() and MEMOLINE() to "extract" a Line of *.CSV
Code: Select all
LOCAL cMemo := MEMOREAD("C:\TEMP\MYFILE.CSV")
LOCAL ii,nMax, aToken, cDelimiter := CHR(9)
LOCAL lUseHeader := .F.
nMax := MlCount( cMemo, 80 )
FOR ii := 1 TO nMax
cLine := MemoLine( cMemo, 80, ii )
aToken := hb_ATokens(cLine, cDelimiter)
IF ii = 1 .AND. lUseHeader
DO_CreateDBF(aToken)
ELSE
DO_Replace(aToken)
ENDIF
NEXT
https://harbour.github.io/doc/harbour.html
have fun
Jimmy
Jimmy
Re: Leer archivo txt delimitado por tabulares
This is the simplest way to that I know of to start
Make sure your table field length is long enough to get length of text file line.
Here your table will have all records one field with line from your text file.
There are other ways to append also where you create fields in the table and the file may have delimiters like commas in a csv file.
Here you would append from &fil file delimited with ","
Your dbf will have each field filled with data from text between commas.
Hope this helps.
Franco
Make sure your table field length is long enough to get length of text file line.
Code: Select all
#include "hmg.ch"
FUNCTION Main()
local fil := 'sam.txt'
AADD( aSTRUCT_L , { 'z1 ' , 'c' , 80, 0 } )
DBCREATE( "your.dbf" , aSTRUCT_L,)
use
use your via "dbfntx"
zap
append from &fil sdf
use
return
There are other ways to append also where you create fields in the table and the file may have delimiters like commas in a csv file.
Here you would append from &fil file delimited with ","
Your dbf will have each field filled with data from text between commas.
Hope this helps.
Franco
All The Best,
Franco
Canada
Franco
Canada
Re: Leer archivo txt delimitado por tabulares
Here is how append from a text file works.
If you have a text csv or prn file that looks like the following.
Your textfile.txt
John, Doe,
Jack, Smith,
Tom, Tam,
Text file end.
You create a table.
YourTable.
field1 character 60 | field2 character 20 |
Table end.
If you INCODE use YourTable then append from "YourTextfile.txt" SDF
You get in your table only field1 has data field2 is empty
field1 ______________________ field2
John, Doe,
Jack, Smith,
Tom, Tam,
If you INCODE use YourTable then append from "YourTextfile.txt" DELIMITED with ","
You get in your table field1 and field 2
field1 _____________________field2
John______________________ Doe
Jack______________________ Smith
Tom______________________ Tam
End example.
I hope this is helping
Franco
If you have a text csv or prn file that looks like the following.
Your textfile.txt
John, Doe,
Jack, Smith,
Tom, Tam,
Text file end.
You create a table.
YourTable.
field1 character 60 | field2 character 20 |
Table end.
If you INCODE use YourTable then append from "YourTextfile.txt" SDF
You get in your table only field1 has data field2 is empty
field1 ______________________ field2
John, Doe,
Jack, Smith,
Tom, Tam,
If you INCODE use YourTable then append from "YourTextfile.txt" DELIMITED with ","
You get in your table field1 and field 2
field1 _____________________field2
John______________________ Doe
Jack______________________ Smith
Tom______________________ Tam
End example.
I hope this is helping
Franco
All The Best,
Franco
Canada
Franco
Canada
Re: Leer archivo txt delimitado por tabulares
Sorry I revised previous post to read more clearly.
All The Best,
Franco
Canada
Franco
Canada