简体   繁体   中英

Save certain strings from a text file in Python

I need to save a certain strings from a text file. I have a list of keywords that I need to save and each keyword is on a single line. An example would be:

name=hydrogen
symbol=h
number=1

I need to save the string hydrogen, h and 1 but I've tried working with the single characters but I couldn't figure out what to do. Could you help?

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))

A simple way to go about this follows. It may or may not be ideal, depending what you really need:

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

where foo.txt is your text file. This will iterate over each line, split it at the first `=.

'a=b=c'.split('=', 1) gets you ['a', 'b=c'] .

For lines that do not contain a = , or that don't have anything afterwards, strip('=', 1) will return a list with just one element, so trying to get the element with index 1 will throw the IndexError that you can treat as you need.

Last, strip() gets rid of whitespace at the start and end of the right-hand string in case you have strings like a = b .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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