日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

文本解析 python 多行,关于python:基于文本的数据格式,支持多行字符串

發(fā)布時(shí)間:2024/7/23 python 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 文本解析 python 多行,关于python:基于文本的数据格式,支持多行字符串 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我搜索支持多行字符串的基于文本的數(shù)據(jù)格式。

JSON不允許多行字符串:

>>> import json

>>> json.dumps(dict(text='first line

second line'))

'{"text":"first line\

second line"}'

我想要的輸出:

{"text":"first line

second line"}

這個(gè)問題是關(guān)于輸入和輸出的。 數(shù)據(jù)格式應(yīng)該可以使用vi,emacs或notepad等編輯器進(jìn)行編輯。

我不在乎是否使用簡(jiǎn)單的引號(hào)"或tripple引號(hào)(如在Python中)"""。

是否有一個(gè)易于人類可讀的文本數(shù)據(jù)交換格式支持這個(gè)?

用例

我想用多行字符串vi編輯數(shù)據(jù)。 如果數(shù)據(jù)是json格式,這不好玩。

你能詳細(xì)說明數(shù)據(jù)格式/目的,即復(fù)雜的結(jié)構(gòu)或一些設(shè)置/配置文件等。

@NabeelAhmed我想用它進(jìn)行配置。 許多應(yīng)用程序發(fā)明了自己的配置語(yǔ)言。 我想避免這種情況。 但是json和ConfigParser并不滿足我。 Json不允許帶換行符的字符串(僅 n)和ConfigParser不允許嵌套數(shù)據(jù)結(jié)構(gòu)。 我缺少的下一件事:驗(yàn)證(但這是一個(gè)不同的主題)。 親愛的Nabeel,如果有遺漏,請(qǐng)留下新評(píng)論。

我想如果你可以替換轉(zhuǎn)儲(chǔ)結(jié)果,那么結(jié)果應(yīng)該是正確的。data = json.dumps(dict(text='first line

second line')) data = data.replace('\

', '

') print(data)

我認(rèn)為你應(yīng)該考慮YAML格式。它支持塊表示法,它能夠保留這樣的換行符

data: |

There once was a short man from Ealing

Who got on a bus to Darjeeling

It said on the door

"Please don't spit on the floor"

So he carefully spat on the ceiling

對(duì)于任何類型的編程語(yǔ)言都有很多解析器,包括python(即pyYaml)。

還有一個(gè)巨大的優(yōu)勢(shì),任何有效的JSON都是YAML。

贊成為打油詩(shī)。

你評(píng)論的答案:

I want to use it for configuration. A lot of applications invent

their own configuration language. I want to avoid this. But json and

ConfigParser don't satisfy me. Json does not allow strings with

newlines (only

) and ConfigParser does not allow nested data

structures. Next thing that I am missing: Validation (But this is a

different topic).

ConfigParser,ConfigObj或YAML(PyYAML)有3個(gè)主要選項(xiàng) - 每個(gè)選項(xiàng)都有其特定的優(yōu)點(diǎn)和缺點(diǎn)。對(duì)于您的用例即配置文件,所有3個(gè)優(yōu)于JSON。

現(xiàn)在進(jìn)一步說,哪一個(gè)更好取決于你想要在conf文件中存儲(chǔ)什么。

ConfigObj - 用于配置和驗(yàn)證(您的用例):

ConfigObj非常簡(jiǎn)單,然后使用YAML(也就是ConfigParser)。支持默認(rèn)值和類型,還包括驗(yàn)證(優(yōu)于ConfigParser)。

ConfigObj簡(jiǎn)介

When you perform validation, each of the members in your specification

are checked and they undergo a process that converts the values into

the specified type. Missing values that have defaults will be filled

in, and validation returns either True to indicate success or a

dictionary with members that failed validation. The individual checks

and conversions are performed by functions, and adding your own check

function is very easy.

附:是的,它允許多行值。

有用的網(wǎng)址:

一個(gè)簡(jiǎn)短的ConfigObj教程

ConfigObj 5簡(jiǎn)介和參考

在比較YAML與ConfigParser和ConfigObj之間有可靠的SO答案:

什么更好,ConfigObj或ConfigParser?

ConfigObj / ConfigParser與使用YAML for Python設(shè)置文件

如果你對(duì)標(biāo)記開銷沒問題,可以使用ElementTree(標(biāo)準(zhǔn)庫(kù))或lxml的XML:

數(shù)據(jù)

Lorem

Ipsum

Dolor

腳本

import xml.etree.ElementTree

root = xml.etree.ElementTree.parse('data.xml').getroot()

for child in root:

print(child.tag, child.attrib, child.text)

產(chǎn)量

string {} Lorem

Ipsum

Dolor

如果文件僅由Python使用(忽略交換),您可以簡(jiǎn)單地將數(shù)據(jù)放在python腳本文件中并將其作為模塊導(dǎo)入:

數(shù)據(jù)

datum_1 =""" lorem

ipsum

dolor

"""

datum_list = [1,"""two

liner"""]

datum_dict = {"key": None,"another": [None, 42.13]}

datum_tuple = ("anything","goes")

腳本

from data import *

d = [e for e in locals() if not e.startswith("__")]

print( d )

for k in d:

print( k, locals()[k] )

產(chǎn)量

['datum_list', 'datum_1', 'datum_dict', 'datum_tuple']

datum_list [1, 'two

liner']

datum_1 ?lorem

ipsum

dolor

datum_dict {'another': [None, 42.13], 'key': None}

datum_tuple ('anything', 'goes')

更新:

代碼與字典理解

from data import *

d = {e:globals()[e] for e in globals() if not e.startswith("__")}

for k in d:

print( k, d[k] )

ini格式也支持多行字符串; Python stdlib中的configparser可以處理它。請(qǐng)參閱https://docs.python.org/3/library/configparser.html#supported-ini-file-structure。

如果您使用的是Python 2,我實(shí)際上認(rèn)為json可以滿足您的需求。您可以在解碼和加載json時(shí)使用string-escape對(duì)其進(jìn)行解碼和編碼:

import json

config_dict = {

'text': 'first line

second line',

}

config_str = json.dumps(config_dict).decode('string-escape')

print config_str

config_dict = json.loads(config_str.encode('string-escape'))

print config_dict

輸出:

{"text":"first line

second line"}

{u'text': u'first line

second line'}

因此,您可以使用解碼后的字符串來編輯JSON,包含換行符,并在讀取時(shí),只需使用string-escape進(jìn)行編碼即可獲取字典。

不確定我是否正確理解了你的問題,但你不是要求這樣的事嗎?

my_config = {

"text":"""first line

second line"""

}

print my_config

這是什么樣的數(shù)據(jù)格式? 你展示了Python的來源。 這已經(jīng)是用戶"句柄"的答案。

@guettli哦,沒錯(cuò),我的觀點(diǎn)與"處理"用戶完全相同。

總結(jié)

以上是生活随笔為你收集整理的文本解析 python 多行,关于python:基于文本的数据格式,支持多行字符串的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。