简体   繁体   English

如何在我的 js 代码中使用此 local.txt 文件?

[英]How can I use this local .txt file in my js code?

I have succeeded in using MechanicalSoup (Python library) to scrape the data from my school's website(s) and have created two.txt files (upcomingFixturesData.txt and resultsFixturesData.txt) which I would like to use to fill the content of one of my tables (I'm using Gatsby as a single-page site).我已经成功地使用 MechanicalSoup(Python 库)从我学校的网站上抓取数据并创建了两个.txt 文件(upcomingFixturesData.txt 和 resultsFixturesData.txt),我想用它来填充其中一个的内容我的表格(我将 Gatsby 用作单页站点)。 The.txt documents both look like this: .txt 文件看起来都是这样的:

22/03/2022 22/03/2022
4:15pm下午 4:15
Hockey曲棍球
Hockey v SchoolName: U16 (a, leave at 3.00pm) Hockey v SchoolName: U16 (a, 下午 3:00 出发)

22/03/2022 22/03/2022
10:00am 10:00 AM
Fives五人制
Fives National Championships: U18 (SchoolName, leave at 9.00am)全国五人制锦标赛:U18(校名,上午 9 点出发)

22/03/2022 22/03/2022
4:00pm 4:00 PM
Netball投球
Netball: Leavers v Common Room (h)无板篮球:毕业生诉公共休息室(h)

Currently, my js code is using dummy data for the sports tables:目前,我的 js 代码正在为运动表使用虚拟数据:

export default function SportsSection() {
  return (
    <Wrapper>
      <RowWrapper>
        <Description>Fixtures</Description>
        <Row
          number="1"
          title="Football"
          subtitle="Boys' U14 A and B team v SchoolName (away, leave at 12:20pm)"
          time="14:30"
        />
        <Row
          number="2"
          title="Hockey"
          subtitle="Hockey v SchoolName: U16 and 2st XI (away, depart 2.00pm)"
          time="15:45"
        />
        <Row
          number="3"
          title="Netball"
          subtitle="Netball: A and B team v SchoolName (home)"
          time="16:20"
        />
        <Row
          number="4"
          title="Rowing"
          subtitle="J15 vs SchoolName (Location)"
          time="15:30"
        />
      </RowWrapper>
    </Wrapper>
  )
}

I made a similar table to show each pupil's timetable, which was much easier as it was already a.json file, and I was able to use a 'map':我制作了一个类似的表格来显示每个学生的时间表,这更容易,因为它已经是一个 .json 文件,而且我能够使用“地图”:

export default function TimetableSection() {
  const data = require("../../data/scraped/timetableWeekData.json")

  var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
  var d = new Date()
  var dayName = days[d.getDay()]
  var tmrName = days[d.getDay() + 1]

  var hrs = new Date().getHours()

  const todayData = data.events.filter(
    // event => event["start-date-display"] == "Today"
    event => event.description.includes(hrs <= 20 ? dayName : tmrName)
  )

  return (
    <Wrapper>
      <RowWrapper>
        <Description>Timetable {hrs <= 20 ? "Today" : "Tomorrow"}</Description>
        {todayData.map((item, index) => (
          <Row
            number={index + 1}
            title={item.subject}
            color={item.colour}
            subtitle={item.chairperson + ", " + item.location}
            time={item.starttime}
          />
        ))}
      </RowWrapper>
    </Wrapper>
  )
}

Is there any way that I could map the.txt files in order to populate each of my table rows, or failing that, how else could I go about using this data (which is inside the project file system) in my javascript code?有没有什么办法可以 map the.txt 文件来填充我的每个表行,或者如果失败了,我还能如何 go 在我的 javascript 代码中使用这些数据(在项目文件系统中)?

Thanks so much for your help in advance!非常感谢您的提前帮助!

simply like this, but with this function, your txt require correct structure to mapping就像这样,但是对于这个 function,您的 txt 需要正确的结构才能映射

const fs = require("fs");
const _ = require("lodash");

const fileTxt = fs.readFileSync("./test.txt", { encoding: "utf-8" });

const eachData = fileTxt.split(/^\r\n/gm); // split for each object

const resultArr = [];
let number = 0;
eachData.forEach((data) => {
  const arrOneData = data.split(/\r\n/gm); // split for each object property
  resultArr.push({
    number,
    date: arrOneData[0],
    time: arrOneData[1],
    title: arrOneData[2],
    subtitle: arrOneData[3],
  });
  number += 1;
});

console.log("resultArr", resultArr);

// resultArr [
//   {
//     number: 0,
//     date: '22/03/2022',
//     time: '4:15pm',
//     title: 'Hockey',
//     subtitle: 'Hockey v SchoolName: U16 (a, leave at 3.00pm)'
//   },
//   {
//     number: 1,
//     date: '22/03/2022',
//     time: '10:00am',
//     title: 'Fives',
//     subtitle: 'Fives National Championships: U18 (SchoolName, leave at 9.00am)'
//   },
//   {
//     number: 2,
//     date: '22/03/2022',
//     time: '4:00pm',
//     title: 'Netball',
//     subtitle: 'Netball: Leavers v Common Room (h)'
//   }
// ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM