python中的configparser问题
事实上,我正沉浸在工作中。我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
似乎 ConfigParser 并不是完成这项工作的最佳工具。您可以自己实现解析逻辑,例如:
或者您可以编写一些其他逻辑来划分文件中的列表。这取决于您想要在文件中表示它的方式
Seems like ConfigParser is not the best tool for the job. You may implement the parsing logic youself something like:
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
是的,配置解析器可能不是最好的选择......但如果你真的想,试试这个:
Yea, the config parser probably isn't the best choice...but if you really want to, try this:
json
模块 提供了更好的支持列表在配置文件中。尝试使用
JSON
来代替 ConfigParser(无列表支持)格式。既然你的问题听起来像家庭作业,我会建议一个丑陋的黑客。使用
str.split()
和float()
从 a 中解析列表配置文件。假设文件x.conf
包含:您可以使用以下方式解析它:(
可以从配置文件中删除列表周围的括号,使
[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.Since your question smells like homework, I'll suggest an ugly hack. Use
str.split()
andfloat()
to parse a list from a configuration file. Suppose the filex.conf
contains:You can parse it with:
(The brackets around the list could be dropped from the configuration file, making the
[1:-1]
hack unnecessary )