2htdp/batch-io库包含有用的 read-csv-file
将 CSV 文件读入列表的过程。它以文件名作为参数。不幸的是,它不接受包含 CSV 的字符串作为其参数。假设我的字符串变量中有一个 CSV,并且我想使用 read-csv-file 来解析它。有没有办法避免将 CSV 保存到文件中以便能够解析 CSV?
文档说:
reads the standard input device (until closed) or the content of file f and produces it as a list of lists of comma-separated values.
可能可以利用标准输入功能来实现此目的,但我不知道如何继续这个想法。
最佳答案
2htdp 库适合初学者,因此不如其他 csv 库灵活。因此,您有两个选择:
batch-io
提供了simulate-file
,它与您想要的类似,但没有什么比将字符串包装在函数中将其放入文件更干净的了喜欢的对象:
> (simulate-file read-csv-file "test,test,test")
(list (list "test" "test" "test"))
- 使用
csv-reading
(csv-reading
必须下载,但只需(需要csv-reading)
并继续解决它给出的错误你):
#lang racket
(require csv-reading)
> (csv->list "test,test,test")
(list (list "test" "test" "test"))
如果batch-io
更通用,它将接受字符串?
或输入端口?
,但事实并非如此。
关于csv - 如何使用 read-csv-file 来读取字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64755347/