在Python中保存文本文件中的某些字符串

时间:2018-02-08 08:04:06

标签: python string python-2.7 text char

我需要从文本文件中保存某些字符串。我有一个我需要保存的关键字列表,每个关键字都在一行上。 一个例子是:

name=hydrogen
symbol=h
number=1

我需要保存字符串氢,h和1但我已尝试使用单个字符,但我无法弄清楚要做什么。你能帮忙吗?

import urllib2
import re

nome = ["hydrogen","helium","lithium","berilium"]
f = open('tavolaperiodica.txt','w')
for x in range(0, 3):
    data = urllib2.urlopen("https://en.wikipedia.org/w/index.php?action=raw&title=Template:Infobox%20" + nome[x])
    #data = data.split("\n") # then split it into lines


    for line in data:
        #print line
        f.write(line)

    f.write("\n\n\nNew\n\n\n")


f.close()

infile = "tavolaperiodica.txt"
outfile = "cleaned_file.txt"

delete_list = ["|", "}", "{"]
fin = open(infile)
fout = open(outfile, "w+")
for line in fin:
    for word in delete_list:
        line = line.replace(word, "")
    fout.write(line)
fin.close()
fout.close()

lines = []
pat = re.compile(r"\binorganic\b")
with open('cleaned_file.txt') as fp:
    line = fp.readline()
    cnt = 1
    while line:
        #print("Line {}: {}".format(cnt, line.strip()))
        line = fp.readline()
        lines.append(line.strip())
        if pat.search(line) != None:
            print("Found it.")

        cnt += 1

paramethers = ["name","symbol","number"]
index = 0
par = list("")
value =  list("")
pr = open('prova.txt', 'w')
for i in range(0, 3):
    print("linea: ", i)
    print(len(lines[i]))
    x = 0
    while x < len(lines[i]):
        print(x)

        if lines[i][x] == "=":
            index = x
            print("Uguale", index)
            y = 0
            for y in range(index+1, len(lines[i])):
                print("y ", y)
                #value.append(lines[i][y])
                z = 0
                while z > y:
                    print("cisono")
                    par.append(lines[i][z])
                    for d in range(0, len(paramethers)):
                        if par == paramethers:
                               value.append(lines[i][y])
                    d+=1
                z+=1
            y+=1
        else:
            print("eskere")

        x = x + 1
    value.append("\n\n")
i+=1
print(value)
pr.write(''.join(value))

1 个答案:

答案 0 :(得分:1)

下面是一个简单的方法。根据您的真正需要,它可能是也可能不是理想的:

values = []
with open('foo.txt', 'rt') as f:
    for line in f:
        try:
            values.append(
                line.split('=', 1)[1].strip())
        except IndexError:
            # depending on your needs; if you want to ignore lines
            # that are not key=value, replace the append with a
            # pass
            values.append('')
for v in values:
    print "got:", v

其中foo.txt是您的文本文件。这将迭代每一行,在第一个`=分割它。

'a=b=c'.split('=', 1)为您提供['a', 'b=c']

对于不包含=或之后没有任何内容的行,strip('=', 1)将返回仅包含一个元素的列表,因此尝试获取索引为1的元素将抛出您可以根据需要处理的IndexError

最后,如果您有strip()之类的字符串,a = b会删除右侧字符串开头和结尾的空格。