简体   繁体   English

使用 python 格式化多行字符串

[英]format multiline string with python

I need to format the below shown multiple line string in python. I've tried many ways but they don't end up well.我需要在 python 中格式化下面显示的多行字符串。我尝试了很多方法,但结果并不理想。

AMAZON
IPHONE: 700
SAMSUNG: 600

=============

WALMART
IPHONE: 699

===========

ALIBABA
SONY: 500

So, the above data represent the online store and it's price of a mobile with its brand.因此,以上数据代表在线商店及其品牌手机的价格。 I need to add these to a database.我需要将这些添加到数据库中。 So, it should be like this所以,它应该是这样的

-------------------
AMAZON | IPHONE | 700
-------------------
AMAZON | SAMSUNG | 600
-------------------
WALMART | IPHONE | 699
-------------------
ALIBABA | SONY | 500
-------------------

I need to format the above text and store it in a database table.我需要格式化上面的文本并将其存储在数据库表中。

What I have tried?我试过什么? I tried to split the multiple lines and create a dictionary more likely to be JSON. But It doesn't end well.我试图拆分多行并创建一个更有可能是 JSON 的字典。但它并没有很好地结束。 But it takes only one line.但它只需要一根线。 If there is some other easy approach share me.如果还有其他简单的方法,请与我分享。 Please help me with this!请在这件事上给予我帮助!

I made some assumptions:我做了一些假设:

  • the vendor name is always before the products供应商名称始终在产品之前
  • at least === as separator between vendor entries至少 === 作为供应商条目之间的分隔符
  • empty lines can be ignored空行可以忽略

Working code:工作代码:

str = """
AMAZON
IPHONE: 700
SAMSUNG: 600

=============

WALMART
IPHONE: 699

===========

ALIBABA
SONY: 500
"""

new_entry = True
print("-------------------")
for line in str.split("\n"):
    # assuming first entry is always the vendor name
    if not line.strip():
        continue
    elif new_entry:
        vendor = line.strip()
        new_entry = False
    elif "===" in line:
        new_entry = True
    else:
        product = line.split(":")
        print("{} | {} | {}".format(vendor, product[0].strip(), product[1].strip()))
        print("-------------------")

Output is: Output 是:

-------------------
AMAZON | IPHONE | 700
-------------------
AMAZON | SAMSUNG | 600
-------------------
WALMART | IPHONE | 699
-------------------
ALIBABA | SONY | 500
-------------------

Alternative approach: The vendor name could also be found as being a text line, but without colon.替代方法:供应商名称也可以作为文本行找到,但没有冒号。

answer submitted by @scito is adequate enough, but i am putting mine just in case. @scito 提交的答案已经足够了,但我还是以防万一。 you can use regex, following is a working example:您可以使用正则表达式,以下是一个工作示例:

strng = """
AMAZON
IPHONE: 700
SAMSUNG: 600

=============

WALMART
IPHONE: 699

===========

ALIBABA
SONY: 500

======
"""

multistrng = strng.split("\n") # get each line seperated by \n

import re 

market_re = re.compile('([a-zA-Z]+)') # regex to find market name

phone_re = re.compile(r"([a-zA-Z]+):\s(\d+)") # regex to find phone and its price

js = [] # list to hold all data found

for line in multistrng:
    phone = phone_re.findall(line) # if line contains phone and its price
    if phone:
        js[-1].append(phone[0]) # add phone to recently found marketplace
        continue
    market = market_re.findall(line)
    if market: # if line contains market place name
        js.append([market[0]])
        continue
    else:
        continue # empty lines ignore

# now you have the data in structured manner, you can print or add it to the database

for market in js:
    for product in market[1:]:
        print("---------------------")
        print("{} | {} | {}".format(market[0], product[0], product[1]))

print("---------------------")

output: output:

---------------------
AMAZON | IPHONE | 700
---------------------
AMAZON | SAMSUNG | 600
---------------------
WALMART | IPHONE | 699
---------------------
ALIBABA | SONY | 500
---------------------

data is stored in js list, if you iterate over js, first element in sub-list is market place, and rest is products for that market place.数据存储在 js 列表中,如果你遍历 js,子列表中的第一个元素是市场,rest 是该市场的产品。

[['AMAZON', ('IPHONE', '700'), ('SAMSUNG', '600')], ['WALMART', ('IPHONE', '699')], ['ALIBABA', ('SONY', '500')]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM