0

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))
2
  • you should perhaps add the code you've tried in the question? Commented Feb 8, 2018 at 8:05
  • Just updated the post with the code Commented Feb 8, 2018 at 8:10

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

It gives me this error: print line.split('=', 1)[1].strip() IndexError: list index out of range
I changed line.split('=', 1)[1].strip() with line.split('=', 1)[0].strip() and it seems to work. In output though it gives me just the beginning of the phrase until the "="
you probably have lines with just key= (no value). i'll edit the answer to cope with this.
No worries. If you're going to use python more, check out its many features. You'll probably find easy and quick ways for many problems. Once you get the hang of it, you'll see it was written to make your life easy :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.