English 中文(简体)
试图列入名单
原标题:Python String to list processing

I feel my recently gained knowledge is still not sufficient when it comes to string processing. Please help me solve the below problem statement: (Please note:This is the simpler version of my requirement)

因此,我有一份内容如下的文件:

day=monday,tuesday,wednesday
month=jan,feb,march,april
holiday=thanksgiving,chirstmas

我的字典应当能够读到文档并处理读写法,这样一来,清单中有三个变量,如下:

day --> [ monday , tuesday , wednesday ]
month --> [ jan , feb , march , april ]
holiday --> [ thanksgiving , christmas ]

Please note: As per my requirement, the format for contents in myoption file should be simple. Hence You are free to modify the format of myoption file without changing the content - This is to give you some flexibility.

感谢:

最佳回答

如果您使用。 INI 文档格式,这样可以看一看:

[options]
day = monday,tuesday,wednesday
month = jan,feb,march,april
holiday = thanksgiving,christmas

然后,你可以读到以下档案:

import ConfigParser

parser = ConfigParser.ConfigParser()
parser.read( myoption.ini )
day = parser.get( options , day ).split( , )
month = parser.get( options , month ).split( , )
holiday = parser.get( options , holiday ).split( , )
问题回答

这里简单回答:

s =  day=monday,tuesday,wednesday 
mylist = {}
key, value = s.split( = )
mylist[key] = value.split( , )

print mylist[ day ][0]

Output: monday

http://docs.python.org/library/configparser.html

You can rewrite your file in YAML or XML if you want your application respect a standard. Anyway, if you want to keep your simple format, this should word:

鉴于文件data:

day=monday,tuesday,wednesday
month=jan,feb,march,april
holiday=thanksgiving,chirstmas

我提议:<>python script

f = open("data")
for line in f:
  content = line.split("=")
  #vars to set the variable name as the string found and 
  #[:-1] to remove the new line character
  vars()[content[0]] = content[1][:-1].split(",")
print day
print month
print holiday
f.close()

产出

python main.py 
[ monday ,  tuesday ,  wednesday ]
[ jan ,  feb ,  march ,  april ]
[ thanksgiving ,  chirstmas ]




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签