簡體   English   中英

Python - 格式化字符串

[英]Python - Formatting strings

我有輸入文件:

sun vehicle
one number
two number
reduce command
one speed
five speed
zero speed
speed command
kmh command

我使用了以下代碼:

from collections import OrderedDict
output = OrderedDict()
with open('final') as in_file:
for line in in_file:    
columns = line.split(' ')
if len(columns) >= 2:
    word,tag = line.strip().split()
    if output.has_key(tag) == False:
        output[tag] = [];
    output[tag].append(word)
else:
    print ""
    for k, v in output.items():
        print '<{}> {} </{}>'.format(k, ' '.join(v), k)
    output = OrderedDict()

我得到的輸出為:

<vehicle> sun </vehicle>
<number> one two </number>
<command> reduce speed kmh </command>
<speed> one five zero </speed>

但我的預期輸出應該是:

<vehicle> sun </vehicle>
<number> one two </number>
<command> reduce 
<speed> one five zero </speed>
speed kmh </command>

有人可以幫我解決這個問題嗎?

看起來你想要實現的輸出是不明確的!

在你到達line speed command之前,你可能希望代碼“提前知道” speedcommand的一部分。

要做你想做的事,你需要一個遞歸函數 怎么樣

    for k, v in output.items():
        print  expandElements(k, v,output)

以及你定義的某個地方

    def expandElements(k,v, dic):
        out = '<' +k + '>'
        for i in v:
          # check each item of v for matches in dic.
          # if no match, then out=out+i
          # otherwise expand using a recursive call of expandElements()
          # and out=out+expandElements
        out = out + '<' +k + '>'

看起來你想要輸出某種樹形結構?

您正在使用print '<{}> {} </{}>'.format(k, ' '.join(v), k)進行print '<{}> {} </{}>'.format(k, ' '.join(v), k)因此您的所有輸出都將采用'<{}> {} </{}>'的形式'<{}> {} </{}>'

如果你想嵌套東西,你需要一個嵌套的結構來表示它們。

為了遞歸地解析輸入文件,我將創建一個表示標記的類。 每個標簽都有自己的children 每個子tag.children.append("value")首先是用tag.children.append("value")手動添加的字符串,或者通過調用tag.add_value(tag.name,“value”)。

class Tag:
    def __init__(self, name, parent=None):
        self.name = name
        self.children = []
        self.has_root = True
        self.parent = parent

    def __str__(self):
        """ compose string for this tag (recursivly) """
        if not self.children:
            return self.name
        children_str = ' '.join([str(child) for child in self.children])
        if not self.parent:
            return children_str
        return '<%s>%s</%s>' % (self.name, children_str, self.name)

    @classmethod
    def from_file(cls, file):
        """ create root tag from file """
        obj = cls('root')
        columns = []
        with open(file) as in_file:
            for line in in_file:
                value, tag = line.strip().split(' ')
                obj.add_tag(tag, value)
        return obj

    def search_tag(self, tag):
        """ search for a tag in the children """
        if self.name == tag:
            return self
        for i, c in enumerate(self.children):
            if isinstance(c, Tag) and c.name == tag:
                return c
            elif isinstance(c, str):
                if c.strip() == tag.strip():
                    self.children[i] = Tag(tag, self)
                    return self.children[i]
            else:
                result = c.search_tag(tag)
                if result:
                    return result

    def add_tag(self, tag, value):
         """
         add a value, tag pair to the children

         Firstly this searches if the value is an child. If this is the
         case it moves the children to the new location

         Afterwards it searches the tag in the children. When found
         the value is added to this tag. If not a new tag object 
         is created and added to this Tag. The flag has_root
         is set to False so the element can be moved later.
         """
         value_tag = self.search_tag(value)
         if value_tag and not value_tag.has_root:
             print("Found value: %s" % value)
             if value_tag.parent:
                 i = value_tag.parent.children.index(value_tag)
                 value = value_tag.parent.children.pop(i)
                 value.has_root = True
             else:
                 print("not %s" % value)

         found = self.search_tag(tag)
         if found:
             found.children.append(value)
         else:
             # no root
             tag_obj = Tag(tag, self)
             self.children.append(tag_obj)
             tag_obj.add_tag(tag, value)
             tag_obj.has_root = False

tags = Tag.from_file('final')
print(tags)

我知道在這個例子中,speed-Tag沒有添加兩次。 我希望沒關系。 對不起長代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM