Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get text at line-nr from large texfile using "File f.open"
#1
Sorry this should be very simple but somehow I am overlooking something.

I have a textfile containing 40.000 lines.
I want to get the text on linenumber 1100.

Is it possible to do it without the .getfile function or without putting the whole textfile into an array or Icsv object?

I have this code:

Macro Macro18
Code:
Copy      Help
str s="D:\_TMP\test.txt"
File f.Open(s)
f.SetPos(1100)
f.ReadLine(s)
out s

The problem is, that the SetPos function puts the position at character at character-number, but I want to put the position at line-number 1100 and then extract the line at that position.
Is it possible?

I am assuming that the code beneath accesses the text file 1100 times (line by line).
I want to avoid unnecessary reads.

Macro Macro18
Code:
Copy      Help
int i
for i 0 1100    
,if(!f.ReadLine(s))
,,break
,if(i=1100)
,,out s ;; I want this line (1100)
#2
Quote:Is it possible to do it without the .getfile function or without putting the whole textfile into an array or Icsv object?
Not possible. Would need to create an index of line offsets, but better use Sqlite instead.
#3
Function ConvertTextFileToSqliteDatabase
Code:
Copy      Help
;/
function $textFile $dbFile $tableName $columnName [$columnType]

;Converts text file to Sqlite database file.

;textFile - text file. Data in it must be stored as a simple multiline list.
;dbFile - Sqlite database file to create. Can have .db3 or other extension.
;tableName - name of table to create in the database file.
;columnName - name of the data column in the table.
;columnType - type of the data column. If used, can be TEXT, INTEGER or REAL.

;REMARKS
;In the database file this function creates a table that has 2 columns:
;;;id - numbers 1,2,3... . Its type is INTEGER PRIMARY KEY.
;;;columnName - lines from the text file. Its type is columnType.


str s ss.getfile(textFile)
if(dir(dbFile)) del- dbFile

Sqlite x.Open(dbFile)
x.Exec(F"BEGIN TRANSACTION;CREATE TABLE {tableName}(id INTEGER PRIMARY KEY, {columnName} {columnType});")
foreach s ss
,x.Exec(F"INSERT INTO {tableName}({columnName}) VALUES('{s.SqlEscape}')")
x.Exec("END TRANSACTION")

err+ end _error
example
Macro Macro2134
Code:
Copy      Help
;convert text file to sqlite
str textFile="$my qm$\test\test.txt"
str dbFile="$my qm$\test\test.db3" ;;Sqlite database
ConvertTextFileToSqliteDatabase textFile dbFile "items" "data" "TEXT"

;get line id=2 from Sqlite database file
Sqlite y.Open(dbFile)
ARRAY(str) a
int i=2
y.Exec(F"SELECT data FROM items WHERE id={i}" a)
if(a.len) out a[0 0]
else out F"id {i} does not exist"
#4
wow! Thank you!
This is impressive!


Forum Jump:


Users browsing this thread: 1 Guest(s)