CSV 到 Json 库在节点 js 中不起作用
CSV to Json library not working in node js
我正在尝试从 FTP 服务器解析一个 csv 文件。当文件只有一行数据时就会出现问题。如果文件有超过 1 行数据,它工作正常。下面的代码完成了这项工作。调试后我发现 fromString 方法没有 return 任何东西,但是从套接字中读取数据成功。感谢任何帮助
async function getFileData(ftpConnection, filename) {
return new Promise((resolve, reject) => {
ftpConnection.get(filename, (err, socket) => {
const products = [];
if (err) {
console.log(err)
reject(null);
}
socket.on('data', data => {
csv({
headers: HEADERS,
delimiter: [';'],
})
.fromString(data.toString('latin1'))
.subscribe((product) => {
// Product is empty when its only one line
products.push(product);
})
});
socket.on('close', closingError => {
if (closingError) {
console.error('[Error]: ', filename);
reject(null)
} else {
console.log("PRODUCTs")
console.log(products)
resolve(products);
}
});
socket.resume();
});
});
}
默认情况下,csv2json 假设您有一个 header 描述您的 csv 文件。如果只有一行,则此行被认为是 header,因此您没有数据。
您可以通过将 noheader
选项设置为 true
来避免此行为。
csv ( {noheader:true} ).fromString ('1,2,3').then ((csv) => { console.log (csv); });
const CSV2JSON = async(dumb, editDumb, headers) => {
try {
log(`\n\nStarting CSV2JSON - Current directory: ${__dirname} - Please wait..`)
if (!dumb && !editDumb && !headers) {
var {dumb, editDumb, headers} = require('minimist')(process.argv.slice(2))
}
const options = {
trim: true,
delimiter: '|',
quote: '\'',
escape: '\'',
fork: true,
// output: "csv",
// eslint-disable-next-line max-len
headers: Array.isArray(headers) ? headers : JSON.parse(headers)
// headers: headers,
// noheader: true
}
await new Promise((resolve, reject) => {
let counter = 0
lineReader.eachLine(dumb, async(line, last) => {
log(line)
counter++
let json = await csv(options).fromString(headers + '\n\r' + line)
json = JSON.stringify(json[0])
if (counter === 1) {
// Check for first Line
json = `[${json},`
} else if (last) {
// Check for last Line
json = `${json}]`
resolve()
} else {
// Check for inbetween Line
json = `${json},`
}
await fs.appendFile(editDumb, json)
})
})
} catch (e) {
throw new BaseError(`Error while converting CSV to JSON - Error: ${e}`)
}
}
我正在尝试从 FTP 服务器解析一个 csv 文件。当文件只有一行数据时就会出现问题。如果文件有超过 1 行数据,它工作正常。下面的代码完成了这项工作。调试后我发现 fromString 方法没有 return 任何东西,但是从套接字中读取数据成功。感谢任何帮助
async function getFileData(ftpConnection, filename) {
return new Promise((resolve, reject) => {
ftpConnection.get(filename, (err, socket) => {
const products = [];
if (err) {
console.log(err)
reject(null);
}
socket.on('data', data => {
csv({
headers: HEADERS,
delimiter: [';'],
})
.fromString(data.toString('latin1'))
.subscribe((product) => {
// Product is empty when its only one line
products.push(product);
})
});
socket.on('close', closingError => {
if (closingError) {
console.error('[Error]: ', filename);
reject(null)
} else {
console.log("PRODUCTs")
console.log(products)
resolve(products);
}
});
socket.resume();
});
});
}
默认情况下,csv2json 假设您有一个 header 描述您的 csv 文件。如果只有一行,则此行被认为是 header,因此您没有数据。
您可以通过将 noheader
选项设置为 true
来避免此行为。
csv ( {noheader:true} ).fromString ('1,2,3').then ((csv) => { console.log (csv); });
const CSV2JSON = async(dumb, editDumb, headers) => {
try {
log(`\n\nStarting CSV2JSON - Current directory: ${__dirname} - Please wait..`)
if (!dumb && !editDumb && !headers) {
var {dumb, editDumb, headers} = require('minimist')(process.argv.slice(2))
}
const options = {
trim: true,
delimiter: '|',
quote: '\'',
escape: '\'',
fork: true,
// output: "csv",
// eslint-disable-next-line max-len
headers: Array.isArray(headers) ? headers : JSON.parse(headers)
// headers: headers,
// noheader: true
}
await new Promise((resolve, reject) => {
let counter = 0
lineReader.eachLine(dumb, async(line, last) => {
log(line)
counter++
let json = await csv(options).fromString(headers + '\n\r' + line)
json = JSON.stringify(json[0])
if (counter === 1) {
// Check for first Line
json = `[${json},`
} else if (last) {
// Check for last Line
json = `${json}]`
resolve()
} else {
// Check for inbetween Line
json = `${json},`
}
await fs.appendFile(editDumb, json)
})
})
} catch (e) {
throw new BaseError(`Error while converting CSV to JSON - Error: ${e}`)
}
}