簡體   English   中英

將列表值解壓縮為文本

[英]Unpacking list values to a text

我要列出一個清單,我想將其解壓縮為一部分文字。 目標是最終將文本導出到txt文件,然后使用VPN將其實現到我們的數據庫。

文字是這樣的:

'"name": "----"',

'"lei": " "',

'"parentId": "0"',

'"status": "ACTIVE"',

'"type": "1"',

'"country": "----"',

'"region": "1"',

'"address": "----"',

'"domains": "None"'

它從我變成一個列表的DataFrame開始,因此基本上每個列表中的每個值都與其他列表中具有相同索引的相同值相關。

目標是要有318個文本,就像我寫的那樣,每個文本將具有3個值。

列表是- "Name""Country""Address"

我曾想過使用帶有for循環的map,但是我不確定如何實現這一點。

有什么建議么?

format_string = """
'"name": "{}"',
'"lei": " "',
'"parentId": "0"',
'"status": "ACTIVE"',
'"type": "1"',
'"country": "{}"',
'"region": "1"',
'"address": "{}"',
'"domains": "None"'
"""

for data in zip(Name, Country, Address):
    print(format_string.format(*data))

這就是你想要的嗎?

我認為您可以使用python的字符串格式來實現您要嘗試的操作,並且如您所建議的那樣,使用map對所有文件執行任務。

想法是編寫一種通用文本,就像您介紹的文本一樣,用“ {0}”,“ {1}”,“ {2}”字段替換“ ----”字段。 然后,將text.format(name, country, adress)將滿山遍野,按照這個順序,與內容namecountryadress領域。

要映射它,您還需要使用splat運算符( * ): text.format(*tuple_of_arguments)來解壓縮包含參數的元組。

因此,一種可接受的解決方案是使用以下形式的代碼:

list_of_tuples = [
        ('nameA', 'countryA', 'adressA'),
        ('nameB', 'countryB', 'adressB')
    ] # fill this, and maybe use a generator instead (e.g. using zip)
for e in map(lambda x : text.format(*x), list_of_tuples):
    # do what you want of the formatted text stored in e

我希望這有幫助 !

暫無
暫無

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

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