解析此文件的最佳方法是什么?

时间:2022-02-22 23:14:15

I was just wondering if anyone knew of a good way that I could parse the file at the bottom of the post.

我只是想知道是否有人知道我可以在帖子底部解析文件的好方法。

I have a database setup with the correct tables for each section eg Refferal Table,Caller Table,Location Table. Each table has the same columns that are show in the file below

我有一个数据库设置,每个部分都有正确的表格,例如Refferal Table,Caller Table,Location Table。每个表都具有相同的列,显示在下面的文件中

I would really like something that is fairly genetic so if the file layout changes it won't mess me around to much. At the moment I am just reading the file in a line at a time and just using a case statement to check which section i'm in.

我真的很想要一些相当遗传的东西,所以如果文件布局发生变化,它就不会让我感到困惑。目前我只是在一行中读取文件,只是使用case语句来检查我所在的部分。

Is anyone able to help me with this?

有人能帮我这个吗?

PS. I am using VB but C# or anything else will be fine, also the x's in the document are just personal info I have blanked

PS。我使用VB但C#或其他任何东西都没问题,文件中的x也只是个人信息我已经空白

Thanks, Nathan

File:--->

DIAL BEFORE YOU DIG
Call 1100, Fax 1300 652 077
PO Box 7710 MELBOURNE, VIC 8004

Utilities are requested to respond within 2 working days and reference the Sequence number.

[REFFERAL DETAILS]
FROM=                 Dial Before You Dig - Web
TO=                   Technical Services
UTILITY ID=           xxxxxx
COMPANY=              {Company Name}
ENQUIRY DATE=         02/10/2008 13:53
COMMENCEMENT DATE=    06/10/2008
SEQUENCE NO=          xxxxxxxxx
PLANNING=             No

[CALLER DETAILS]
CUSTOMER ID=          403552
CONTACT NAME=         {Name of Contact}
CONTACT HOURS=        0
COMPANY=              Underground Utility Locating
ADDRESS=              {Address}
SUBURB=               {Suburb}
STATE=                {State}
POSTCODE=             4350
TELEPHONE=            xxxxxxxxxx
MOBILE=               xxxxxxxxxx
FAX TYPE=             Private
FAX NUMBER=           xxxxxxxxxx
PUBLIC ADDRESS=       xxxxxxxxxx
PUBLIC TELEPHONE=
EMAIL ADDRESS=        {Email Address}

[LOCATION DETAILS]
ADDRESS=              {Location Address}
SUBURB=               {Location Suburb}
STATE=                xxx
POSTCODE=             xxx
DEPOSITED PLAN NO=    0
SECTION & HUNDRED NO= 0
PROPERTY PHONE NO=
SIDE OF STREET=       B
INTERSECTION=         xxxxxx
DISTANCE=             0-200m B
ACTIVITY CODE=        15
ACTIVITY DESCRIPTION= xxxxxxxxxxxxxxxxxx
MAP TYPE=             StateGrid
MAP REF=              Q851_63
MAP PAGE=
MAP GRID 1=
MAP GRID 2=
MAP GRID 3=
MAP GRID 4=
MAP GRID 5=
GPS X COORD=
GPS Y COORD=
PRIVATE/ROAD/BOTH=    B
TRAFFIC AFFECTED=     No
NOTIFICATION NO=      3082321
MESSAGE=              entire intersection of Allora-Clifton rd , Hillside
rd and merivale st

MOCSMESSAGE=          Digsafe generated referral

Notice: Please DO NOT REPLY TO THIS EMAIL as it has been automatically generated and replies are not monitored. Should you wish to advise Dial Before You Dig of any issues with this enquiry, please Call 1100

(See attached file: 3082321_LLGDA94.GML)

4 个解决方案

#1


5  

Google has the answers, once you know that the file-format is called '.ini'

一旦你知道文件格式被称为'.ini',Google就会得到答案

Edit: That is, it's an .ini plus some extra leading/trailing gunk.

编辑:也就是说,它是一个.ini加上一些额外的领先/尾随的垃圾。

#2


4  

You could read each line of the file sequentially. Each line is essentially a name value pair. Place each value in a map (hash table) keyed by name. Use a map for each section. When done parsing the file you'll have maps containing all the name value pairs. Iterate over each map and populate your database tables.

您可以按顺序读取文件的每一行。每一行基本上都是一个名称对。将每个值放在按名称键入的映射(哈希表)中。为每个部分使用地图。完成解析文件后,您将拥有包含所有名称值对的映射。迭代每个映射并填充数据库表。

#3


2  

I would head to Python for any type of string parsing like this. I'm not sure how much of this information you want to retain, but I would perhaps use Python's split() function to split on = to get rid of the equals sign, then strip the whitespace out of the second piece of the pie.

我会前往Python进行任何类型的字符串解析。我不确定你想要保留多少这些信息,但我可能会使用Python的split()函数拆分=去除等号,然后从第二块饼中去掉空白。

First, I would mask out the header/footer info I know I don't need, then do something akin to the following:

首先,我会掩盖我知道我不需要的页眉/页脚信息,然后做类似于以下的事情:

Let's take a chunk and save it in test1.txt:

让我们拿一块并将其保存在test1.txt中:

ADDRESS=              {Location Address}
SUBURB=               {Location Suburb}
STATE=                xxx
POSTCODE=             xxx
DEPOSITED PLAN NO=    0
SECTION & HUNDRED NO= 0
PROPERTY PHONE NO=

Here's a small python snippet:

这是一个小python片段:

>>> f = open("test1.txt", "r")
>>> l = f.readlines()
>>> l = [line.split('=') for line in l]
>>> for line in l:
    print line

['ADDRESS', '{Location Address}']
['SUBURB', '{Location Suburb}']
['STATE', 'xxx']
['POSTCODE', 'xxx']
['DEPOSITED PLAN NO', '0']
['SECTION & HUNDRED NO', '0']
['PROPERTY PHONE NO', '']

This would essentially give you a [Column, Value] tuple you could use to insert the data into your database (after escaping all strings, etc etc, SQL Injection warning).

这基本上会为您提供一个[Column,Value]元组,您可以使用它将数据插入数据库(在转义所有字符串等之后,SQL注入警告)。

This is assuming the email input and your DB will have the same column names, but if they didn't, it'd be fairly trivial to set up a column mapping using a dictionary. On the flip side, if the email and columns are in sync, you don't need to know the names of the columns to get the parsing down.

这假设电子邮件输入和您的数据库将具有相同的列名称,但如果他们没有,则使用字典设置列映射是相当简单的。另一方面,如果电子邮件和列同步,则无需知道列的名称即可进行解析。

You could iterate through the pseudo-dictionary and print out each key-value pair in the right spot in your parameterized sql string.

您可以遍历伪字典并在参数化的sql字符串中的正确位置打印出每个键值对。

Hope this helps!

希望这可以帮助!

Edit: While this is in Python, C#/VB.net should have the same/similar abilities.

编辑:虽然这是在Python中,但C#/ VB.net应该具有相同/相似的能力。

#4


1  

Using f As StreamReader = File.OpenText("sample.txt")
    Dim g As String = "undefined"
    Do
        Dim s As String = f.ReadLine
        If s Is Nothing Then Exit Do
        s = s.Replace(Chr(9), " ")
        If s.StartsWith("[") And s.EndsWith("]") Then
            g = s.Substring("[".Length, s.Length - "[]".Length)
        Else
            Dim ss() As String = s.Split(New Char() {"="c}, 2)
            If ss.Length = 2 Then
                Console.WriteLine("{0}.{1}={2}", g, Trim(ss(0)), Trim(ss(1)))
            End If
        End If
    Loop
End Using

#1


5  

Google has the answers, once you know that the file-format is called '.ini'

一旦你知道文件格式被称为'.ini',Google就会得到答案

Edit: That is, it's an .ini plus some extra leading/trailing gunk.

编辑:也就是说,它是一个.ini加上一些额外的领先/尾随的垃圾。

#2


4  

You could read each line of the file sequentially. Each line is essentially a name value pair. Place each value in a map (hash table) keyed by name. Use a map for each section. When done parsing the file you'll have maps containing all the name value pairs. Iterate over each map and populate your database tables.

您可以按顺序读取文件的每一行。每一行基本上都是一个名称对。将每个值放在按名称键入的映射(哈希表)中。为每个部分使用地图。完成解析文件后,您将拥有包含所有名称值对的映射。迭代每个映射并填充数据库表。

#3


2  

I would head to Python for any type of string parsing like this. I'm not sure how much of this information you want to retain, but I would perhaps use Python's split() function to split on = to get rid of the equals sign, then strip the whitespace out of the second piece of the pie.

我会前往Python进行任何类型的字符串解析。我不确定你想要保留多少这些信息,但我可能会使用Python的split()函数拆分=去除等号,然后从第二块饼中去掉空白。

First, I would mask out the header/footer info I know I don't need, then do something akin to the following:

首先,我会掩盖我知道我不需要的页眉/页脚信息,然后做类似于以下的事情:

Let's take a chunk and save it in test1.txt:

让我们拿一块并将其保存在test1.txt中:

ADDRESS=              {Location Address}
SUBURB=               {Location Suburb}
STATE=                xxx
POSTCODE=             xxx
DEPOSITED PLAN NO=    0
SECTION & HUNDRED NO= 0
PROPERTY PHONE NO=

Here's a small python snippet:

这是一个小python片段:

>>> f = open("test1.txt", "r")
>>> l = f.readlines()
>>> l = [line.split('=') for line in l]
>>> for line in l:
    print line

['ADDRESS', '{Location Address}']
['SUBURB', '{Location Suburb}']
['STATE', 'xxx']
['POSTCODE', 'xxx']
['DEPOSITED PLAN NO', '0']
['SECTION & HUNDRED NO', '0']
['PROPERTY PHONE NO', '']

This would essentially give you a [Column, Value] tuple you could use to insert the data into your database (after escaping all strings, etc etc, SQL Injection warning).

这基本上会为您提供一个[Column,Value]元组,您可以使用它将数据插入数据库(在转义所有字符串等之后,SQL注入警告)。

This is assuming the email input and your DB will have the same column names, but if they didn't, it'd be fairly trivial to set up a column mapping using a dictionary. On the flip side, if the email and columns are in sync, you don't need to know the names of the columns to get the parsing down.

这假设电子邮件输入和您的数据库将具有相同的列名称,但如果他们没有,则使用字典设置列映射是相当简单的。另一方面,如果电子邮件和列同步,则无需知道列的名称即可进行解析。

You could iterate through the pseudo-dictionary and print out each key-value pair in the right spot in your parameterized sql string.

您可以遍历伪字典并在参数化的sql字符串中的正确位置打印出每个键值对。

Hope this helps!

希望这可以帮助!

Edit: While this is in Python, C#/VB.net should have the same/similar abilities.

编辑:虽然这是在Python中,但C#/ VB.net应该具有相同/相似的能力。

#4


1  

Using f As StreamReader = File.OpenText("sample.txt")
    Dim g As String = "undefined"
    Do
        Dim s As String = f.ReadLine
        If s Is Nothing Then Exit Do
        s = s.Replace(Chr(9), " ")
        If s.StartsWith("[") And s.EndsWith("]") Then
            g = s.Substring("[".Length, s.Length - "[]".Length)
        Else
            Dim ss() As String = s.Split(New Char() {"="c}, 2)
            If ss.Length = 2 Then
                Console.WriteLine("{0}.{1}={2}", g, Trim(ss(0)), Trim(ss(1)))
            End If
        End If
    Loop
End Using