python中的configparser问题

发布于 2024-08-05 14:50:54 字数 556 浏览 12 评论 0原文

事实上,我正沉浸在工作中。我想将 txt 文件导入到我的 python 程序中,该程序应该有两个整数列表。

以下程序工作正常,但我需要在 configparser 的帮助下导入列表“a”和“b”。

如果有人帮助我,那就太好了!

我是Python初学者,所以请尝试以简单的方式回答......!

程序如下:

a=[5e6,6e6,7e6,8e6,8.5e6,9e6,9.5e6,10e6,11e6,12e6]

p=[0.0,0.001,0.002,0.003,0.004,0.005,0.006,0.007,0.008,0.009,0.01,0.015,0.05,0.1,0.15,0.2]

b=0

x=0

while b<=10:

    c=a[b]
    x=0

    print '\there is the outer loop\n',c


    while x<=15:

        k=p[x]

        print'here is the inner loop\n',k

        x=x+1

    b=b+1

Actually I am stuck in my work. I want to import a txt file into my python program which should have two lists of intergers.

The following program is working fine but I need to import the list 'a' and 'b' with the help of configparser.

It will be so nice if some one help me with it!

I am a begineer in python so please try to answer in an easy way...!

The program is as follow:

a=[5e6,6e6,7e6,8e6,8.5e6,9e6,9.5e6,10e6,11e6,12e6]

p=[0.0,0.001,0.002,0.003,0.004,0.005,0.006,0.007,0.008,0.009,0.01,0.015,0.05,0.1,0.15,0.2]

b=0

x=0

while b<=10:

    c=a[b]
    x=0

    print '\there is the outer loop\n',c


    while x<=15:

        k=p[x]

        print'here is the inner loop\n',k

        x=x+1

    b=b+1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

百合的盛世恋 2024-08-12 14:50:54

似乎 ConfigParser 并不是完成这项工作的最佳工具。您可以自己实现解析逻辑,例如:

a, b = [], []
with open('myfile', 'r') as f:
    for num, line in enumerate(f.readlines()):
        if num >= 10: 
            b.push(line)
        else:
            a.push(line)

或者您可以编写一些其他逻辑来划分文件中的列表。这取决于您想要在文件中表示它的方式

Seems like ConfigParser is not the best tool for the job. You may implement the parsing logic youself something like:

a, b = [], []
with open('myfile', 'r') as f:
    for num, line in enumerate(f.readlines()):
        if num >= 10: 
            b.push(line)
        else:
            a.push(line)

or you can make up some other logic to devide the lists in your file. It depends on the way you want to represent it in you file

洋洋洒洒 2024-08-12 14:50:54

是的,配置解析器可能不是最好的选择......但如果你真的想,试试这个:

import unittest
from ConfigParser import SafeConfigParser
from cStringIO import StringIO

def _parse_float_list(string_value):
    return [float(v.strip()) for v in string_value.split(',')]

def _generate_float_list(float_values):
    return ','.join(str(value) for value in float_values)

def get_float_list(parser, section, option):
    string_value = parser.get(section, option)
    return _parse_float_list(string_value)

def set_float_list(parser, section, option, float_values):
    string_value = _generate_float_list(float_values)
    parser.set(section, option, string_value)

class TestConfigParser(unittest.TestCase):
    def setUp(self):
        self.a = [5e6,6e6,7e6,8e6,8.5e6,9e6,9.5e6,10e6,11e6,12e6]
        self.p = [0.0,0.001,0.002,0.003,0.004,0.005,0.006,0.007,0.008,0.009,0.01,0.015,0.05,0.1,0.15,0.2]

    def testRead(self):
        parser = SafeConfigParser()
        f = StringIO('''[values]
a:   5e6,   6e6,   7e6,   8e6,
   8.5e6,   9e6, 9.5e6,  10e6,
    11e6,  12e6
p: 0.0  , 0.001, 0.002,
   0.003, 0.004, 0.005,
   0.006, 0.007, 0.008,
   0.009, 0.01 , 0.015,
   0.05 , 0.1  , 0.15 ,
   0.2
''')
        parser.readfp(f)
        self.assertEquals(self.a, get_float_list(parser, 'values', 'a'))
        self.assertEquals(self.p, get_float_list(parser, 'values', 'p'))

    def testRoundTrip(self):
        parser = SafeConfigParser()
        parser.add_section('values')
        set_float_list(parser, 'values', 'a', self.a)
        set_float_list(parser, 'values', 'p', self.p)

        self.assertEquals(self.a, get_float_list(parser, 'values', 'a'))
        self.assertEquals(self.p, get_float_list(parser, 'values', 'p'))

if __name__ == '__main__':
    unittest.main()

Yea, the config parser probably isn't the best choice...but if you really want to, try this:

import unittest
from ConfigParser import SafeConfigParser
from cStringIO import StringIO

def _parse_float_list(string_value):
    return [float(v.strip()) for v in string_value.split(',')]

def _generate_float_list(float_values):
    return ','.join(str(value) for value in float_values)

def get_float_list(parser, section, option):
    string_value = parser.get(section, option)
    return _parse_float_list(string_value)

def set_float_list(parser, section, option, float_values):
    string_value = _generate_float_list(float_values)
    parser.set(section, option, string_value)

class TestConfigParser(unittest.TestCase):
    def setUp(self):
        self.a = [5e6,6e6,7e6,8e6,8.5e6,9e6,9.5e6,10e6,11e6,12e6]
        self.p = [0.0,0.001,0.002,0.003,0.004,0.005,0.006,0.007,0.008,0.009,0.01,0.015,0.05,0.1,0.15,0.2]

    def testRead(self):
        parser = SafeConfigParser()
        f = StringIO('''[values]
a:   5e6,   6e6,   7e6,   8e6,
   8.5e6,   9e6, 9.5e6,  10e6,
    11e6,  12e6
p: 0.0  , 0.001, 0.002,
   0.003, 0.004, 0.005,
   0.006, 0.007, 0.008,
   0.009, 0.01 , 0.015,
   0.05 , 0.1  , 0.15 ,
   0.2
''')
        parser.readfp(f)
        self.assertEquals(self.a, get_float_list(parser, 'values', 'a'))
        self.assertEquals(self.p, get_float_list(parser, 'values', 'p'))

    def testRoundTrip(self):
        parser = SafeConfigParser()
        parser.add_section('values')
        set_float_list(parser, 'values', 'a', self.a)
        set_float_list(parser, 'values', 'p', self.p)

        self.assertEquals(self.a, get_float_list(parser, 'values', 'a'))
        self.assertEquals(self.p, get_float_list(parser, 'values', 'p'))

if __name__ == '__main__':
    unittest.main()
对风讲故事 2024-08-12 14:50:54

json 模块 提供了更好的支持列表在配置文件中。
尝试使用 JSON 来代替 ConfigParser(无列表支持)格式。

JSON(JavaScript 对象表示法)是一种轻量级数据交换格式。人类很容易阅读和书写。机器很容易解析和生成。它基于 JavaScript 编程语言的子集,标准 ECMA-262 第三版 - 1999 年 12 月。JSON 是一种完全独立于语言的文本格式,但使用 C 系列语言(包括 C)程序员熟悉的约定、C++、C#、Java、JavaScript、Perl、Python 等。这些属性使 JSON 成为理想的数据交换语言。

既然你的问题听起来像家庭作业,我会建议一个丑陋的黑客。使用 str.split()float() 从 a 中解析列表配置文件。假设文件 x.conf 包含:

[sect1]
a=[5e6,6e6,7e6,8e6,8.5e6,9e6,9.5e6,10e6,11e6,12e6]

您可以使用以下方式解析它:(

>>> import ConfigParser
>>> cf=ConfigParser.ConfigParser()
>>> cf.read(['x.conf'])
['x.conf']
>>> [float(s) for s in cf.get('sect1','a')[1:-1].split(',')]
[5000000.0, 6000000.0, 7000000.0, 8000000.0, 8500000.0, 9000000.0, 9500000.0, 10000000.0, 11000000.0, 12000000.0]
>>> 

可以从配置文件中删除列表周围的括号,使 [1:-1]黑客不必要)

The json module provides better support for lists in configuration files.
Instead of the ConfigParser (no list support) format, try using JSON for this purpose.

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

Since your question smells like homework, I'll suggest an ugly hack. Use str.split() and float() to parse a list from a configuration file. Suppose the file x.conf contains:

[sect1]
a=[5e6,6e6,7e6,8e6,8.5e6,9e6,9.5e6,10e6,11e6,12e6]

You can parse it with:

>>> import ConfigParser
>>> cf=ConfigParser.ConfigParser()
>>> cf.read(['x.conf'])
['x.conf']
>>> [float(s) for s in cf.get('sect1','a')[1:-1].split(',')]
[5000000.0, 6000000.0, 7000000.0, 8000000.0, 8500000.0, 9000000.0, 9500000.0, 10000000.0, 11000000.0, 12000000.0]
>>> 

(The brackets around the list could be dropped from the configuration file, making the [1:-1] hack unnecessary )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文