Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中保存文本文件中的某些字符串_Python_String_Python 2.7_Text_Char - Fatal编程技术网

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

在Python中保存文本文件中的某些字符串,python,string,python-2.7,text,char,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,

我需要保存文本文件中的特定字符串。我有一个需要保存的关键字列表,每个关键字都在一行上。 例如:

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))
导入urllib2
进口稀土
nome=[“氢”、“氦”、“锂”、“铍”]
f=打开('tavolaneopidica.txt','w')
对于范围(0,3)内的x:
data=urlib2.urlopen(“https://en.wikipedia.org/w/index.php?action=raw&title=Template:Infobox%20“+nome[x])
#data=data.split(“\n”)#然后将其拆分为行
对于行输入数据:
#打印行
f、 写(行)
f、 写入(“\n\n新\n\n\n”)
f、 关闭()
infle=“tavolnoniodica.txt”
outfile=“cleaned_file.txt”
删除列表=[“|”、“}”、“{”]
翅片=打开(填充)
fout=打开(输出文件,“w+”)
对于fin中的行:
对于删除列表中的单词:
行=行。替换(单词“”)
四、写(行)
财务结束()
fout.close()
行=[]
pat=re.compile(r“\binorganic\b”)
打开('cleaned_file.txt')作为fp:
line=fp.readline()
cnt=1
while line:
#打印(“行{}:{}.”格式(cnt,Line.strip())
line=fp.readline()
lines.append(line.strip())
如果pat.search(行)!=无:
打印(“找到了。”)
cnt+=1
参数=[“名称”、“符号”、“编号”]
索引=0
PAR=列表(“”)
值=列表(“”)
pr=open('prova.txt','w')
对于范围(0,3)内的i:
打印(“linea:,i)
打印(len(行[i]))
x=0
而xy时:
印刷品(“西索诺”)
PAR.AppEnter(行[i] [Z])
对于范围(0,len(参数))中的d:
如果PAR= =参数:
value.append(第[i][y]行)
d+=1
z+=1
y+=1
其他:
印刷品(“埃斯克尔”)
x=x+1
值。追加(“\n\n”)
i+=1
打印(值)
pr.write(“”.join(值))

一个简单的方法如下。它可能是理想的,也可能不是理想的,这取决于您真正需要什么:

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的元素将抛出
索引器,您可以根据需要处理该索引器


最后,
strip()
去掉右边字符串开头和结尾的空格,以防出现类似
a=b

的字符串。您可能应该添加您在问题中尝试过的代码?刚刚用代码更新了帖子。它给了我以下错误:print line.split('=',1)[1]。strip()索引器错误:列表索引超出范围我更改了line.split('=',1)[1].strip()和line.split('=',1)[0].strip()似乎可以工作。在输出中,虽然它只给出短语的开头,直到“=”为止,您可能有只带有
键=
(无值)的行。我会编辑答案来解决这个问题。不用担心。如果你想更多地使用python,请查看它的许多功能。你可能会找到解决许多问题的简单快捷的方法。一旦你掌握了窍门,你会发现它是为了让你的生活变得轻松而编写的:)