I have no idea on how to do this. Where should I start? I have googled this and not one result came up on how to pull a random line from a text file.
我不知道如何做到这一点。我应该从哪里开始?我用谷歌搜索了这个,并没有提出如何从文本文件中拉出随机行的结果。
The only thing I have found is https://github.com/chrisinajar/node-rand-line, however it doesn't work. How can I read a random line from a text file?
我发现的唯一的东西是https://github.com/chrisinajar/node-rand-line,但它不起作用。如何从文本文件中读取随机行?
3 个解决方案
#1
9
You would probably want to look at the node.js standard library function for reading files, fs.readFile, and end up with something along the lines of:
您可能希望查看node.js标准库函数来读取文件fs.readFile,最后得到以下内容:
//note this will be async
function getRandomLine(filename){
fs.readFile(filename, function(err, data){
if(err) throw err;
var lines = data.split('\n');
/*do something with */ lines[Math.floor(Math.random()*lines.length)];
})
}
If reading the whole thing and splitting isn't an option, then maybe have a look at this stack overflow for ideas.
如果阅读整个事情并且拆分不是一个选项,那么可能要看看这个堆栈溢出的想法。
#2
3
I don't have Node handy to test code, so I can't give you exact code, but I would do something like this:
我没有Node方便测试代码,所以我不能给你确切的代码,但我会做这样的事情:
- Get the file size in bytes, pick a random byte offset
- 获取文件大小(以字节为单位),选择随机字节偏移量
- Open the file as a stream
- 以流形式打开文件
- Use this snippet to emit lines (or readline, but last I used it had a nasty bug where it essentially didn't work)
- 使用这个片段发出行(或readline,但最后我使用它有一个讨厌的bug,它本质上不起作用)
- Keep track of your position in the file as you read. As you pass your chosen offset, select that line and return it.
- 在阅读时跟踪您在文件中的位置。当您传递所选的偏移量时,选择该行并将其返回。
Note that this isn't entirely random. Longer lines will be weighted more heavily, but it is the only way to do it without reading the whole file to get a count of lines.
请注意,这并非完全随机。较长的行将被加权更多,但它是唯一的方法,无需读取整个文件来获得行数。
This method allows you to get a "random" line without keeping the whole file in memory.
此方法允许您获取“随机”行而不将整个文件保留在内存中。
#3
0
I can give you a suggestion as I don't have any demo code
我可以给你一个建议,因为我没有任何演示代码
- Read the file line by line using
buffered reader
- 使用缓冲读取器逐行读取文件
- Store every line in a String array
- 将每一行存储在String数组中
- Create a method
int returnRandom(arraySize)
- 创建方法int returnRandom(arraySize)
- Pass the array size in to the function
- 将数组大小传递给函数
- Calculate a random number between
0
toarraySize
- 计算0到arraySize之间的随机数
- Return the random number
- 返回随机数
- Print the given index from your string array
- 从字符串数组中打印给定的索引
#1
9
You would probably want to look at the node.js standard library function for reading files, fs.readFile, and end up with something along the lines of:
您可能希望查看node.js标准库函数来读取文件fs.readFile,最后得到以下内容:
//note this will be async
function getRandomLine(filename){
fs.readFile(filename, function(err, data){
if(err) throw err;
var lines = data.split('\n');
/*do something with */ lines[Math.floor(Math.random()*lines.length)];
})
}
If reading the whole thing and splitting isn't an option, then maybe have a look at this stack overflow for ideas.
如果阅读整个事情并且拆分不是一个选项,那么可能要看看这个堆栈溢出的想法。
#2
3
I don't have Node handy to test code, so I can't give you exact code, but I would do something like this:
我没有Node方便测试代码,所以我不能给你确切的代码,但我会做这样的事情:
- Get the file size in bytes, pick a random byte offset
- 获取文件大小(以字节为单位),选择随机字节偏移量
- Open the file as a stream
- 以流形式打开文件
- Use this snippet to emit lines (or readline, but last I used it had a nasty bug where it essentially didn't work)
- 使用这个片段发出行(或readline,但最后我使用它有一个讨厌的bug,它本质上不起作用)
- Keep track of your position in the file as you read. As you pass your chosen offset, select that line and return it.
- 在阅读时跟踪您在文件中的位置。当您传递所选的偏移量时,选择该行并将其返回。
Note that this isn't entirely random. Longer lines will be weighted more heavily, but it is the only way to do it without reading the whole file to get a count of lines.
请注意,这并非完全随机。较长的行将被加权更多,但它是唯一的方法,无需读取整个文件来获得行数。
This method allows you to get a "random" line without keeping the whole file in memory.
此方法允许您获取“随机”行而不将整个文件保留在内存中。
#3
0
I can give you a suggestion as I don't have any demo code
我可以给你一个建议,因为我没有任何演示代码
- Read the file line by line using
buffered reader
- 使用缓冲读取器逐行读取文件
- Store every line in a String array
- 将每一行存储在String数组中
- Create a method
int returnRandom(arraySize)
- 创建方法int returnRandom(arraySize)
- Pass the array size in to the function
- 将数组大小传递给函数
- Calculate a random number between
0
toarraySize
- 计算0到arraySize之间的随机数
- Return the random number
- 返回随机数
- Print the given index from your string array
- 从字符串数组中打印给定的索引