如何将数据转换为csv文件

pqwbnv8z  于 2023-09-27  发布在  其他
关注(0)|答案(1)|浏览(129)

我的任务是从一个xml文件中获取数据,并将该文件中的一些数据转换为csv文件。现在我已经设法得到的数据(找到所有的素数,并记录到控制台),但我不知道我如何才能将此转换为csv文件,请帮助!!
下面是我的HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Badges Task</title>
</head>
<body>

    <h1>Task</h1>
    <button id="action">Download csv</button>

    <script src="app.js"></script>
</body>
</html>

这是我的JavaScript

const getData = function() {

    fetch("task.xml").then(response => {
        return response.text()
    }).then(xmlString => {
        const xmlDocument = new DOMParser().parseFromString(xmlString, 'text/xml');
    
        const badges = xmlDocument.querySelectorAll('badge');

        console.log(xmlDocument)
    
        for(const badge of badges) {
            const id = badge.getAttribute('number');
            const date = badge.getAttribute('issued').slice(0,11);

            console.log(date)
    
            let isPrime = true;
    
            if(id <= 1) {
                console.log("1 is neither prime nor composite number")
            } else if(id > 1) {
                for(let i = 2; i < id; i++) {
                    if(id % i == 0) {
                        isPrime = false;
                        break;
                    }
                }
            }
    
            if(isPrime) {
                console.log(`${id} is a prime number`)
            } 

         
            
        }

   
    });

  

}

const btn = document.getElementById('action');
btn.addEventListener('click', getData);

下面是一些XML数据文件的列表,比这个要长得多

<?xml version="1.0" encoding="utf-8"?>
<accessControl>
    <badges>
        <badge number="1" passcode="535335" issued="02/03/2016 00:00:00"/>
        <badge number="2" passcode="988798" issued="23/02/1997 00:00:00"/>
        <badge number="3" passcode="823603" issued="22/08/2003 00:00:00"/>
        <badge number="4" passcode="767876" issued="12/11/1999 00:00:00"/>
        <badge number="5" passcode="905922" issued="19/11/2009 00:00:00"/>
        <badge number="6" passcode="773912" issued="11/08/2015 00:00:00"/>
        <badge number="7" passcode="689032" issued="24/01/2003 00:00:00"/>
        <badge number="8" passcode="158703" issued="03/01/2010 00:00:00"/>
        <badge number="9" passcode="877498" issued="10/11/2011 00:00:00"/>
        <badge number="10" passcode="960573" issued="19/06/2000 00:00:00"/>
        <badge number="11" passcode="936346" issued="16/10/2018 00:00:00"/>
        <badge number="12" passcode="472985" issued="19/09/2008 00:00:00"/>
        <badge number="13" passcode="113143" issued="28/07/2010 00:00:00"/>
        <badge number="14" passcode="512691" issued="20/11/2012 00:00:00"/>
        <badge number="15" passcode="112630" issued="02/03/2022 00:00:00"/>
        <badge number="16" passcode="656349" issued="19/12/2021 00:00:00"/>
        <badge number="17" passcode="919450" issued="03/11/2002 00:00:00"/>
        <badge number="18" passcode="359069" issued="28/12/2001 00:00:00"/>
      </badges>
     <failedaccesses>
        <accessattempt badge="2347" accessedDate="08/10/2022 00:00:00">
            <passcodeentered value="560775"/>
        </accessattempt>
        <accessattempt badge="2617" accessedDate="24/05/2023 00:00:00">
            <passcodeentered value="993700"/>
        </accessattempt>
        <accessattempt badge="1111" accessedDate="03/07/2023 00:00:00">
            <passcodeentered value="846599"/>
        </accessattempt>
        <accessattempt badge="300" accessedDate="13/09/2022 00:00:00">
            <passcodeentered value="308523"/>
        </accessattempt>
        <accessattempt badge="1509" accessedDate="09/09/2022 00:00:00">
            <passcodeentered value="149089"/>
        </accessattempt>
        <accessattempt badge="218" accessedDate="05/12/2022 00:00:00">
            <passcodeentered value="140284"/>
        </accessattempt>
        <accessattempt badge="1513" accessedDate="04/10/2022 00:00:00">
            <passcodeentered value="791493"/>
        </accessattempt>
        <accessattempt badge="1718" accessedDate="27/04/2023 00:00:00">
            <passcodeentered value="258773"/>
        </accessattempt>
        <accessattempt badge="2214" accessedDate="16/04/2023 00:00:00">
            <passcodeentered value="845822"/>
        </accessattempt>
    </failedaccesses>
</accessControl>
iqjalb3h

iqjalb3h1#

有很多方法可以生成CSV文件。
您可以手动完成,只需生成一个CSV格式的字符串,如下所示:

let csv = 'id, date\n';
for (badge of badges) {
    const id = badge.getAttribute('number');
    const date = badge.getAttribute('issued').slice(0,11);

    csv += `${id}, ${date}\n`
}
return csv

如果你想要更健壮的东西,有很多包可以做到这一点。
例如,我以前使用过fast-csv,它可以与npm一起安装。然后,您可以生成一个CSV,内容如下:

import { format } from '@fast-csv/format';

const csvStream = format({ headers: true });

for (badge of badges) {
    // assuming you already got id and date
    csvStream.write({ id: id, date: date });
}
csvStream.end();
return csvStream.read();

Fast-CSV文档提供了许多创建CSV文件的有用示例:参见here

下载CSV文件

生成CSV文件的字符串表示后,可以使用here描述的方法下载它。

相关问题