JavaScript/Node.JS - 如何提取要读取的 txt 文件的变量列表中的值?
JavaScript/Node.JS - How to extract a value in a list of variables of a txt file to read?
我有一个包含如下变量列表的 txt 文件:
$VARIABLE1 = 'VALUE1';
$VARIABLE2 = 'VALUE2';
$VARIABLE3 = 'VALUE3';
$VARIABLE4 = 'VALUE4';
我只想提取我想使用 javascript 查找的变量的值(例如:查找 $VARIABLE3 => VALUE3 的值)。
有人可以帮忙吗?
在 Python 中,我找到了一种使用如下函数的方法:
def findValue():
import re
valueToFind = []
lineNum = 0
pattern = re.compile("string to find", re.IGNORECASE)
with open(FILE_PATH, 'rt') as myFile:
for line in myFile:
lineNum += 1
if pattern.search(line) is not None:
valueToFind.append((lineNum, line.rstrip('\n')))
for find in valueToFind:
string = find[1].split("'")
return string[1]
提前致谢。
您可以使用 readline
模块逐行读取文件。
const fs = require('fs');
const readline = require('readline');
const { once } = require('events');
async function getVariables(file) {
const stream = fs.createReadStream(file);
const lineReader = readline.createInterface({
input: stream
})
const variables = {};
lineReader.on('line', line => {
const [key, value] = line.split('=').map(item => item.trim())
variables[key] = value.split("'")[1];
})
// wait until the whole file is read
await once(stream, 'end');
return variables;
}
(async() => {
const variables = await getVariables('./file.txt')
console.log(variables['$VARIABLE4']) // VALUE4
})();
如果您希望在找到特定变量后就停下来,您可以这样做。
我将使用另一行 reader 使用流,split2
const split2 = require('split2');
async function findVariable(file, variable) {
const stream = fs.createReadStream(file, { highWaterMark: 5 } );
const lineReader = stream.pipe(split2())
let result = null;
lineReader.on('data', async function (line) {
const [key, value] = line.split('=').map(item => item.trim())
if(key === variable) {
result = value.split("'")[1];
stream.destroy();
}
})
// wait until the whole file is closed
await once(stream, 'close');
return result;
}
(async() => {
const value4 = await findVariable('./file.txt', '$VARIABLE4')
// null if not found
console.log(value4) // VALUE4
})();
我有一个包含如下变量列表的 txt 文件:
$VARIABLE1 = 'VALUE1';
$VARIABLE2 = 'VALUE2';
$VARIABLE3 = 'VALUE3';
$VARIABLE4 = 'VALUE4';
我只想提取我想使用 javascript 查找的变量的值(例如:查找 $VARIABLE3 => VALUE3 的值)。 有人可以帮忙吗?
在 Python 中,我找到了一种使用如下函数的方法:
def findValue():
import re
valueToFind = []
lineNum = 0
pattern = re.compile("string to find", re.IGNORECASE)
with open(FILE_PATH, 'rt') as myFile:
for line in myFile:
lineNum += 1
if pattern.search(line) is not None:
valueToFind.append((lineNum, line.rstrip('\n')))
for find in valueToFind:
string = find[1].split("'")
return string[1]
提前致谢。
您可以使用 readline
模块逐行读取文件。
const fs = require('fs');
const readline = require('readline');
const { once } = require('events');
async function getVariables(file) {
const stream = fs.createReadStream(file);
const lineReader = readline.createInterface({
input: stream
})
const variables = {};
lineReader.on('line', line => {
const [key, value] = line.split('=').map(item => item.trim())
variables[key] = value.split("'")[1];
})
// wait until the whole file is read
await once(stream, 'end');
return variables;
}
(async() => {
const variables = await getVariables('./file.txt')
console.log(variables['$VARIABLE4']) // VALUE4
})();
如果您希望在找到特定变量后就停下来,您可以这样做。
我将使用另一行 reader 使用流,split2
const split2 = require('split2');
async function findVariable(file, variable) {
const stream = fs.createReadStream(file, { highWaterMark: 5 } );
const lineReader = stream.pipe(split2())
let result = null;
lineReader.on('data', async function (line) {
const [key, value] = line.split('=').map(item => item.trim())
if(key === variable) {
result = value.split("'")[1];
stream.destroy();
}
})
// wait until the whole file is closed
await once(stream, 'close');
return result;
}
(async() => {
const value4 = await findVariable('./file.txt', '$VARIABLE4')
// null if not found
console.log(value4) // VALUE4
})();