Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.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_Arrays_String - Fatal编程技术网

Python 数组值作为字符串

Python 数组值作为字符串,python,arrays,string,Python,Arrays,String,我目前有一个for循环,它将替换文本文件中的字符串,但我希望它们使用我已经定义的变量替换这些字符串 k_constant_vec = [.15707963267,.2221441469,.31415926535,.35124073655,.44428829381,.88857658763,1.33286488145]; for t in range(1, len(k_constant_vec)): infile = open('/home/john/projects/simplec

我目前有一个for循环,它将替换文本文件中的字符串,但我希望它们使用我已经定义的变量替换这些字符串

k_constant_vec = [.15707963267,.2221441469,.31415926535,.35124073655,.44428829381,.88857658763,1.33286488145];

for t in range(1, len(k_constant_vec)):

     infile = open('/home/john/projects/simplecodes/textreplace/ex22.i')

     outfile = open('/home/john/projects/simplecodes/textreplace/newex22-[t].i', 'w')

     replacements = {'k_constant = k_constant_vec[t-1]':'k_constant = k_constant_vec[t]','file_base = out':'file_base = out-[t]'}
        for line in infile:
            for src, target in replacements.iteritems():
                     line = line.replace(src, target)
            outfile.write(line)
            infile.close()
            outfile.close()
我基本上希望使用for循环来创建一组新的.I文件。例如,这将创建标记为newex22-1、newex22-2、ect的7.i文件,每个文件都有不同的
k_常量=k_常量[1]
k_常量=k_常量[2]
,替换了其中的ect字符串


谢谢

您的代码存在一些小错误/样式问题。也不清楚你为什么要使用infle。以下是我认为您正在尝试做的一种更具python风格的方式:

k_constant_vec = [.15707963267,.2221441469,.31415926535,.35124073655,.44428829381,.88857658763,1.33286488145];
file_prefix = '/home/john/projects/simplecodes/textreplace/newex22-[%s].i'
for index in range(len(k_constant_vec)):
  with open(file_prefix % (index + 1), 'wb') as f:
    f.write('k_constant = %s' % k_constant_vec[index])

实际上我已经解决了这个问题,我会在这里发布我所做的,以防有人搜索到类似的问题。我使用了george建议的方法,并在for循环中使用了%s和%

k_constant_vec = [.15707963267,.2221441469,.31415926535,.35124073655,.44428829381,.88857658763,1.33286488145];

for t in range(0,len(k_constant_vec)):
    infile = open('/home/john/projects/simplecodes/textreplace/ex22.i')
    outfile = open('/home/john/projects/simplecodes/textreplace/newex22-'+repr(t)+'.i', 'w')
    replacements = {'k_constant = .15707963267':'k_constant = %s' % k_constant_vec[t],'file_base = out':'file_base = out-'+repr(t)}
    #use default k_constant from ex22.i file
    for line in infile:
        for src, target in replacements.iteritems():
            line = line.replace(src, target)
        outfile.write(line)
    infile.close()
    outfile.close()

使用infle的全部目的是保留文本文件的格式。

您需要使用
.format()
来实现您的目标,因为您不能将变量直接嵌入诸如perl或php
'path/newex22-'+repr(t)这样的字符串中+“.i”
您应该将
k_constant\u vec
中的值放入文件中,还是只放入索引中?谢谢George。我会在一分钟内检查这是否对那个部分有效。是的,jonrsharpe我需要k_常量的值作为字符串在.I文件中,就像它们在数组中一样。这并不能回答问题。我想编辑文件ex22.I并用for循环替换字符串以创建新文件。这只会生成一堆空文件,其中k_constant=%s%k_constant\u vec[index]。