在文本文件中查找空行
Find empy lines in text file
我已经学习球拍几天了,我对这个任务感到困惑,我试图在文本文件中找到空行和 select 一个随机的空行来插入文本"calculation here",这是我目前为止所了解的。
例如:myfile.txt 有内容:
line1
line2
line3
line4
脚本 运行 之后,myfile.txt 现在应该如下所示:
line1
calculation here
line2
line3
line4
或:
line1
line2
line3
calculation here
line4
下面的无效代码:
#lang racket
(define (write-to-file host text) (
with-output-to-file host (
lambda () (
write text))
#:exists 'replace))
(define empty-lines '()) ;store line number of empty line (if any)
(define (file-lines text-file)
(file->lines text-file))
(define (add-to-list line-num)
(set! empty-lines (cons line-num empty-lines)))
(let loop ((l (file-lines "myfile.txt")))
(cond ((null? l) #f)
(else
(printf "~s\n" (first l)) ; just for debugging
(cond ((equal? (first l) "") (add-to-list (first l)))(else #f))
(loop (rest l)))))
;now i need to select a random line from the list of empty-lines.
;and write "calculations here" to that line
我正在使用的读取行方法没有问题,问题是检测并select插入一个随机的空白space来插入我的文本。
在文件中查找 2 个并发的 \n。我很确定球拍中有一种方法可以做到这一点。将这些索引随机存储在列表 select 中,并将第二个 \n 替换为 "calculation here\n".
(define (read-next-line-iter file)
(let ((line (read-line file)))
(unless (eof-object? line)
(display line)
(newline)
(read-next-line-iter file))))
(call-with-input-file "foobar.txt" read-next-line-iter)
http://rosettacode.org/wiki/Read_a_file_line_by_line#Racket
此功能可以帮助您逐行读取文件。
检查长度是否为 0。并将该行替换为注释
给定文件名,您可以使用 file->lines
将其读入行列表。例如:
(for ([line (in-list (file->lines "some-file"))])
(displayln (cond [(zero? (string-length line)) (make-random-line)]
[else line])))
其中 make-random-line
是您为 return 定义的某个函数
随机字符串,就像你说的那样。
上面将整个文件读取到内存中的列表中。对于较大的文件,最好逐行处理。您可以使用 in-lines
序列执行此操作:
(with-input-from-file "some-file"
(thunk
(for ([line (in-lines)])
(displayln (cond [(zero? (string-length line)) (make-random-line)]
[else line])))))
更新
现在我明白了你的问题:
#lang racket
(define lines (file->lines "some-file-name"))
(define empty-line-numbers (for/list ([line (in-list lines)]
[n (in-naturals)]
#:when (zero? (string-length line)))
n))
(define random-line-number (list-ref empty-line-numbers
(random (length empty-line-numbers))))
(for ([line (in-list lines)]
[n (in-naturals)])
(displayln (cond [(= n random-line-number) "SOME NEW STRING"]
[else line])))
我已经学习球拍几天了,我对这个任务感到困惑,我试图在文本文件中找到空行和 select 一个随机的空行来插入文本"calculation here",这是我目前为止所了解的。
例如:myfile.txt 有内容:
line1
line2
line3
line4
脚本 运行 之后,myfile.txt 现在应该如下所示:
line1
calculation here
line2
line3
line4
或:
line1
line2
line3
calculation here
line4
下面的无效代码:
#lang racket
(define (write-to-file host text) (
with-output-to-file host (
lambda () (
write text))
#:exists 'replace))
(define empty-lines '()) ;store line number of empty line (if any)
(define (file-lines text-file)
(file->lines text-file))
(define (add-to-list line-num)
(set! empty-lines (cons line-num empty-lines)))
(let loop ((l (file-lines "myfile.txt")))
(cond ((null? l) #f)
(else
(printf "~s\n" (first l)) ; just for debugging
(cond ((equal? (first l) "") (add-to-list (first l)))(else #f))
(loop (rest l)))))
;now i need to select a random line from the list of empty-lines.
;and write "calculations here" to that line
我正在使用的读取行方法没有问题,问题是检测并select插入一个随机的空白space来插入我的文本。
在文件中查找 2 个并发的 \n。我很确定球拍中有一种方法可以做到这一点。将这些索引随机存储在列表 select 中,并将第二个 \n 替换为 "calculation here\n".
(define (read-next-line-iter file)
(let ((line (read-line file)))
(unless (eof-object? line)
(display line)
(newline)
(read-next-line-iter file))))
(call-with-input-file "foobar.txt" read-next-line-iter)
http://rosettacode.org/wiki/Read_a_file_line_by_line#Racket
此功能可以帮助您逐行读取文件。 检查长度是否为 0。并将该行替换为注释
给定文件名,您可以使用 file->lines
将其读入行列表。例如:
(for ([line (in-list (file->lines "some-file"))])
(displayln (cond [(zero? (string-length line)) (make-random-line)]
[else line])))
其中 make-random-line
是您为 return 定义的某个函数
随机字符串,就像你说的那样。
上面将整个文件读取到内存中的列表中。对于较大的文件,最好逐行处理。您可以使用 in-lines
序列执行此操作:
(with-input-from-file "some-file"
(thunk
(for ([line (in-lines)])
(displayln (cond [(zero? (string-length line)) (make-random-line)]
[else line])))))
更新
现在我明白了你的问题:
#lang racket
(define lines (file->lines "some-file-name"))
(define empty-line-numbers (for/list ([line (in-list lines)]
[n (in-naturals)]
#:when (zero? (string-length line)))
n))
(define random-line-number (list-ref empty-line-numbers
(random (length empty-line-numbers))))
(for ([line (in-list lines)]
[n (in-naturals)])
(displayln (cond [(= n random-line-number) "SOME NEW STRING"]
[else line])))