Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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
用python解析文件_Python - Fatal编程技术网

用python解析文件

用python解析文件,python,Python,买主警告:我会拼写p-y-t-h-o-n,这几乎是我所知道的全部。我试着参加一些在线课程,但在大约20次讲座后,我学到的东西不多,我很久以前就放弃了。所以,我要问的很简单,但我需要帮助: 我有一个具有以下结构的文件: object_name_here: object_owner: - me@my.email.com - user@another.email.com object_id: some_string_here identification: some_oth

买主警告:我会拼写p-y-t-h-o-n,这几乎是我所知道的全部。我试着参加一些在线课程,但在大约20次讲座后,我学到的东西不多,我很久以前就放弃了。所以,我要问的很简单,但我需要帮助:

我有一个具有以下结构的文件:

object_name_here:
  object_owner:
    - me@my.email.com
    - user@another.email.com
  object_id: some_string_here
  identification: some_other_string_here
这个块在同一个文件中重复了数百次。 除了这里的object_name_是唯一和必需的之外,所有其他行可能存在,也可能不存在,电子邮件地址可以是从无到10+个不同的电子邮件地址

我想做的是将这些信息导出到一个平面文件中,比如/etc/passwd,并进行扭曲

例如,我希望上面的块产生一条如下所示的线:

object_name_here:object_owner=me@my_email.com,user@another.email.com:objectid=some_string_here:identification=some_other_string_here
同样,字段的数量或内容字段的长度不是通过任何方式固定的。我确信用python完成这项任务相当容易,但我不知道如何完成。我甚至不知道从哪里开始


最终编辑:好的,我能够编写一个shell脚本(bash、ksh等)来解析信息,但是,当我最初问这个问题时,我的印象是,python有一种更简单的方法来处理统一或半统一的数据结构。我的理解被证明不是很准确。很抱歉浪费了您的时间。

因为您正在用不同的文本子字符串替换文本子字符串,所以这是一个使用正则表达式的非常自然的地方

幸运的是,Python有一个名为
re
的优秀正则表达式库

您可能希望大量使用

re.sub(pattern, repl, string)
请查看此处的文档:

更新:下面是一个如何使用正则表达式库的示例:

#!/usr/bin/env python

import re

body = None
with open("sample.txt") as f:
    body = f.read()

# Replace emails followed by other emails
body = re.sub(" * - ([a-zA-Z.@]*)\n * -", r"\1,", body)

# Replace declarations of object properties
body = re.sub(" +([a-zA-Z_]*): *[\n]*", r"\1=", body)

# Strip newlines
body = re.sub(":?\n", ":", body)

print (body)
示例输出:

$ python example.py
object_name_here:object_owner=me@my.email.com, user@another.email.com:object_id=some_string_here:identification=some_other_string_here

正如jaypb指出的,正则表达式在这里是个好主意。如果您对python 101感兴趣,我将给您一些简单的代码,让您开始使用自己的解决方案

下面的代码是一种快速而肮脏的方法,可以将文件的每六行合并为新文件的一行:

# open some files to read and write
oldfile = open("oldfilename","r")
newfile = open("newfilename","w")

# initiate variables and iterate over the input file
count = 0
outputLine = ""
for line in oldfile:
    # we're going to append lines in the file to the variable outputLine
    # file.readline() will return one line of a file as a string
    # str.strip() will remove whitespace at the beginning and end of a string
    outputLine = outputLine + oldfile.readline().strip()
    # you know your interesting stuff is six lines long, so
    # reset the output string and write it to file every six lines
    if count%6 == 0:
        newfile.write(outputLine + "\n")
        outputLine = ""
    # increment the counter
    count = count + 1

# clean up
oldfile.close()
newfile.close()
这并不完全是你想要做的,但它让你接近。例如,如果您想去掉电子邮件地址开头的“-”,并将其替换为“=”,而不是仅仅附加到
outputLine
上,您可以执行以下操作

if some condition:
    outputLine = outputLine + '=' + oldfile.readline()[3:]
最后一位是python切片,
[3://code>的意思是“在第三个元素之后给我所有东西”,它适用于字符串或列表之类的东西


那会让你开始的。使用google和python文档(例如,google的“python strip”会带您进入页面)了解上面的每一行,然后改变周围的内容以获得您需要的内容

第一个问题:为什么要专门使用python?创建此文件是为了与ansible一起使用。由于ansible运行python,我认为这是自然的选择。第二,但与我的第一个推理不太远,如果有人能解释变量/对象赋值在python中的工作原理,也许这有助于我理解python语言,但我现在主要关心的是解析文件,而不是其他任何东西。同样,该文件在Linux平台中使用。我想用这里提供的工具来解析它,而不是在Windows或AS/400或OpenVMS上。需要某种UNIX衍生工具。这基本上是一个正则表达式操作。我可能会使用
sed
,因此它不是一种编码或教程服务,但我建议先弄清楚如何手动解析它。例如:“我将打开文件,然后逐行检查。如果一行不是以空格开头的,它是一个新对象的名称,因此我将创建一个新对象。如果一行中有一个
@
,则它是一封电子邮件。因此我将它添加到当前对象的所有者”,等等。一旦您有了一个用伪代码编写的合理算法,请尝试用Python实现它。我不知道您一直在使用什么教程,但是非常好。请注意,无论您使用C#、python、sed还是任何其他工具,正则表达式的工作原理都几乎相同。