使用 display-lines-to-file 将列表写入文件
write list to file using display-lines-to-file
我在使显示行到文件工作时遇到问题,这是我尝试过的方法:
(define (list-to-file lst file) (display-lines-to-file lst file
#:exists 'replace
#:separator newline #"\n"))
(list-to-file myList "myfile.txt")
myList 是从另一个文件读取的数据列表,在处理该数据后我需要将列表的内容(包括换行符)写入 myfile.txt
Racket 关键字参数有一个 #:keyword
后跟一个值。问题似乎是:
#:separator newline #"\n"
相反你想要:
#:separator #"\n"
我建议进行一些其他更改:
- 因为
#:separator
的默认值已经是 #"\n"
,请忽略它。
- 您可能需要添加
#:mode 'text
,以便 \n
在 Windows 上自动转换为 \r\n
。
- Racket 几乎允许标识符中的任何内容。利用这一点,按照惯例使用“->”代替“-to-”或“-2-”。
结合所有这些:
(define (list->file lst file)
(display-lines-to-file lst
file
#:exists 'replace
#:mode 'text))
我在使显示行到文件工作时遇到问题,这是我尝试过的方法:
(define (list-to-file lst file) (display-lines-to-file lst file
#:exists 'replace
#:separator newline #"\n"))
(list-to-file myList "myfile.txt")
myList 是从另一个文件读取的数据列表,在处理该数据后我需要将列表的内容(包括换行符)写入 myfile.txt
Racket 关键字参数有一个 #:keyword
后跟一个值。问题似乎是:
#:separator newline #"\n"
相反你想要:
#:separator #"\n"
我建议进行一些其他更改:
- 因为
#:separator
的默认值已经是#"\n"
,请忽略它。 - 您可能需要添加
#:mode 'text
,以便\n
在 Windows 上自动转换为\r\n
。 - Racket 几乎允许标识符中的任何内容。利用这一点,按照惯例使用“->”代替“-to-”或“-2-”。
结合所有这些:
(define (list->file lst file)
(display-lines-to-file lst
file
#:exists 'replace
#:mode 'text))