将文本文件导入 SQL 数据库

发布于 2025-01-08 17:03:37 字数 190 浏览 0 评论 0原文

我有许多单独的文本文件,我想将它们导入到 SQL 数据库中。数据不是用逗号分隔的,因此排除了使用我通过逗号导入数据的想法。但是,数据跨多行。请参阅下面的示例文本文件。请有人告诉我如何导入特定数据,例如编程值和平均值、班次编号等?

在此处输入图像描述

I have a number of separate text files which i would like to import into an SQL database. The data is not comma separted so that rules out using my idea of importing data by comma. However, the data is across a number of rows. See example text file below. Please could anyone advise how i could import specific data such as the programmed and mean values, shift number, etc?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱ヾ落雨殇 2025-01-15 17:03:37

您似乎有一份机器生成的报告。理想的方法是让该机器生成一份不同的报告 - 一份没有“/////”或任何垃圾的报告,只有您想要导入的数据。因此新报告的输出可能如下所示。

shift_num, prog_min, mean_sec, att_sec, adt_min
1, 600, 599, 658, 210
...

但实际上,通常“不可能”获得这样的报告。 (也就是说,机器总是可以做到这一点,但人类通常不愿意。)当这种情况发生时,使用您最喜欢的文本处理语言将报告转换为可用数据。

我喜欢 awk 来处理这类事情。其他人喜欢 Perl。


为了说明这一点,我输入了您报告的副本。 (保存为test.dat。)

ORDER   Nr FG68909                    Q.ty Ordered    99
...
                                         SHIFT Nr. 1

////////
PROGRAMMED                            MEAN

600 min                       JOB TIME        599 sec




AVERAGE Turnaround Time  658 sec
AVERAGE Delivery Time  210 mins

然后我编写了这个awk程序。它对报告的布局做出了很多假设。其中一些可能会在真实数据上失败。

/SHIFT/ {shift = $NF}
/JOB TIME/ {
  programmed = sprintf("%d %s", $1, $2);
  mean = sprintf("%d %s", $(NF-1), $NF);
}
/AVERAGE Turnaround/ {  avg_turnaround = sprintf("%d %s", $(NF-1), $NF);}

# Assumes the line "AVERAGE Delivery" is also the end of the record.
/AVERAGE Delivery/ {  
  avg_delivery = sprintf("%d %s", $(NF-1), $NF);
  printf("%d, '%s', '%s', '%s', '%s'\n", shift, programmed, mean, avg_turnaround, avg_delivery);
  # Clear the vars for the next record.
  shift = "";
  programmed = "";
  mean = "";
  avg_turnaround = "";
  avg_delivery = "";
}

输出。 。 。

$ awk -f test.awk test.dat
1, '600 min', '599 sec', '658 sec', '210 mins'

It looks like you have a machine-generated report. The ideal approach is to have that machine produce a different report--one that has no '/////' or any of that crap, just the data you want to import. So that new report's output might look like this.

shift_num, prog_min, mean_sec, att_sec, adt_min
1, 600, 599, 658, 210
...

In practice, though, it's often not "possible" to get reports like that. (That is, it's always possible for the machine to do it, but often humans are unwilling.) When that happens, use your favorite text-processing language to turn the report into usable data.

I like awk for this kind of stuff. Others like perl.


To illustrate, I keyed in this replica of your report. (Saved as test.dat.)

ORDER   Nr FG68909                    Q.ty Ordered    99
...
                                         SHIFT Nr. 1

////////
PROGRAMMED                            MEAN

600 min                       JOB TIME        599 sec




AVERAGE Turnaround Time  658 sec
AVERAGE Delivery Time  210 mins

Then I wrote this awk program. It makes a lot of assumptions about the layout of your report. Some of them will probably fail on real data.

/SHIFT/ {shift = $NF}
/JOB TIME/ {
  programmed = sprintf("%d %s", $1, $2);
  mean = sprintf("%d %s", $(NF-1), $NF);
}
/AVERAGE Turnaround/ {  avg_turnaround = sprintf("%d %s", $(NF-1), $NF);}

# Assumes the line "AVERAGE Delivery" is also the end of the record.
/AVERAGE Delivery/ {  
  avg_delivery = sprintf("%d %s", $(NF-1), $NF);
  printf("%d, '%s', '%s', '%s', '%s'\n", shift, programmed, mean, avg_turnaround, avg_delivery);
  # Clear the vars for the next record.
  shift = "";
  programmed = "";
  mean = "";
  avg_turnaround = "";
  avg_delivery = "";
}

The output . . .

$ awk -f test.awk test.dat
1, '600 min', '599 sec', '658 sec', '210 mins'
浪漫之都 2025-01-15 17:03:37

您可以用 C# 编写一个简单的应用程序,以使用正则表达式解析文件的内容,将其转换为一行,并在需要的地方插入分号。

You could write a simple application in C# to parse the contents of the file using regex, turn it into one line, and insert semicolons where required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文