简体   繁体   English

键和多行python值

[英]Key and Multi-line python value

I am trying to read a text file or use the ConfigParser on a file with the following structure 我正在尝试读取文本文件或对具有以下结构的文件使用ConfigParser

        Index1 = '''You have been redirected to this page for one of the following 

        reasons:

        Either cookies are not enabled on your browser
        or
        Your network configuration is causing cookies to be lost or not function properly.
        IEEE Xplore requires cookies to maintain sessions and to access licensed content. Cookies are used temporarily to maintain sessions in 
        IEEE Xplore and for no other purpose.'''

        Index2 = '''Internet Explorer 6, 7, or 8
        Click Tools menu.
        Select Internet Options.
        Select Privacy tab. 
        Click the Default button or slide the bar down to 'Medium'.
        Click Ok. '''

I would like to be able to give it the key value Index1 and get the string block after it or iterate through all the Index values and get the blocks after them. 我希望能够为其提供键值Index1并在其后获取字符串块,或者遍历所有Index值并在其后获取块。 I can't seem to read more than one line from the string 我似乎无法从字符串中读取多行

So far I tried 到目前为止,我尝试了

for line in fileinput.input('config.conf'):
    part = line.partition("'''")
    ts = part[0]
    st = part[1]

One approach to solve the problem is to have a config.ini file like 解决问题的一种方法是使用config.ini文件,例如

[Multiline Values]
Index1 : You have been redirected to this page for one of the following
    reasons:
    Either cookies are not enabled on your browser
    or
    Your network configuration is causing cookies to be lost or not function properly.
    IEEE Xplore requires cookies to maintain sessions and to access licensed content. Cookies are used temporarily to maintain sessions in
    IEEE Xplore and for no other purpose.

and read it like: 并像这样阅读:

import ConfigParser
Config = ConfigParser.ConfigParser()
Config.read('config.ini')
val = Config.get('Multiline Values', 'Index1')
print val

output: 输出:

You have been redirected to this page for one of the following
reasons:
Either cookies are not enabled on your browser
or
Your network configuration is causing cookies to be lost or not function properly.
IEEE Xplore requires cookies to maintain sessions and to access licensed content. Cookies are used temporarily to maintain sessions in
IEEE Xplore and for no other purpose.

The header name 'Multiline Values' and key name 'Index1' could be anything. 标头名称“ Multiline Values”和键名称“ Index1”可以是任何值。

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

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