Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 将文件格式化为标记文件_Regex_Python 3.x_Data Structures_Split_Formatting - Fatal编程技术网

Regex 将文件格式化为标记文件

Regex 将文件格式化为标记文件,regex,python-3.x,data-structures,split,formatting,Regex,Python 3.x,Data Structures,Split,Formatting,由于必要性,我将许多文件的内容转储到一个文件中,如下所示: Filename=A1.cpp 描述=A1 CPP Listen=/var/run/A1.cpp Filename=A2.cpp 描述=A2 CPP Listen=/var/run/A2.cpp Write=/home/folder/textfile 正如您可能发现的,关键点会重复它们自己。然而,也有一些例外。可能有通用键和不同的键。 目标是这样的: |文件名|描述|听|写| |A1.cpp | A1 cpp | var/run/A1

由于必要性,我将许多文件的内容转储到一个文件中,如下所示:

Filename=A1.cpp
描述=A1 CPP
Listen=/var/run/A1.cpp
Filename=A2.cpp
描述=A2 CPP
Listen=/var/run/A2.cpp
Write=/home/folder/textfile
正如您可能发现的,关键点会重复它们自己。然而,也有一些例外。可能有通用键和不同的键。 目标是这样的:

|文件名|描述|听|写|
|A1.cpp | A1 cpp | var/run/A1.cpp |-|
|A2.cpp | A2 cpp | var/run/A2.cpp |/home/folder/textfile|
您可能会发现,输出是md格式的。 我对Python相当陌生(3)。 我脑海中的问题可以安排如下:

  • 如何通过
    =
    进行拆分?(我曾尝试使用正则表达式,但有许多情况我无法处理,例如空格、标点符号等)

  • 我应该如何存储它们以获得给定的输出?(Dict,2D数组;始终以文件名开头)


另外,为了简单起见,我展示了我的问题的一个过于简化的版本。

只是在玩正则表达式和
pandas
,这可能会让你开始:

import re, pandas as pd

string = """
Filename=A1.cpp
Description=A1 CPP
Listen=/var/run/A1.cpp
Filename=A2.cpp
Description=A2 CPP
Listen=/var/run/A2.cpp
Write=/home/folder/textfile
"""

# regex for your structure
rx = re.compile(r"""
        ^                                     # start of line
        Filename=(?P<filename>.+)[\n\r]       # Filename
        Description=(?P<description>.+)[\n\r] # Description
        Listen=(?P<listen>.+)[\n\r]           # Listen
        (?:Write=(?P<write>.+)[\n\r])?        # Write, optional
        """, re.VERBOSE | re.MULTILINE)

# column labels
labels = ['Filename', 'Description', 'Listen', 'Write']
rows = [match.groups() for match in rx.finditer(string)]

# formatter for the columns
mdformat = lambda x: '{} |'.format(x)

# create the dataframe
df = pd.DataFrame.from_records(rows, columns=labels)
df.fillna('-', inplace = True)

# print it out using the mdformat function
print(df.to_string(index=False, header = False,
        formatters={'Filename': mdformat, 'Description': mdformat, 
                    'Listen': mdformat, 'Write': mdformat}))


因此,基本上代码会扫描字符串,将结果放入一个列表,并从中创建一个
pandas
dataframe。这是使用格式化程序函数打印出来的。

我们可以说每条记录都以“Filename=”开头吗?是否只有这4个键/值对?如果有空格和标点符号之类的问题。。。你还没有展示它们,所以很难猜测你想要什么。md格式…哪一种?什么是“钥匙”?您做了哪些尝试?是的,每个记录都以Filename=…,通过“=”分隔的目的取决于将来使用的灵活性。因为列名不仅限于四个,还将添加未知列名。由于这个原因,我无法对上一次尝试的列名进行硬编码。给定的格式(条形分隔)可以转换为标记文件。这些尝试都是从硬编码版本开始的,因为键(列名)在普通键的基础上动态变化,我无法通过这种方法处理它们。我试着用“=”来划分左手边和右手边有太多的例外,然后我决定问;有没有一种更明智的方法来处理这种情况。
A1.cpp |  A1 CPP |  /var/run/A1.cpp |                      - |
A2.cpp |  A2 CPP |  /var/run/A2.cpp |  /home/folder/textfile |