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

Python—读取并将字符附加到新的写入

Python—读取并将字符附加到新的写入,python,Python,我有一个名为“Strings.txt”的文件,其中包含以下格式 text = text Strings.txt的小示例: Incoming calls = See the Preferences Incoming message from %@ = Incoming message from %@ Enter Your Password = 我想用Python读取这个文件,插入一些字符/字符串,然后将其写入其他文件,如“Format.strings” 结果将是: /* No comme

我有一个名为“Strings.txt”的文件,其中包含以下格式

text = text  
Strings.txt的小示例:

Incoming calls = See the Preferences
Incoming message from %@ = Incoming message from %@
Enter Your Password = 
我想用Python读取这个文件,插入一些字符/字符串,然后将其写入其他文件,如“Format.strings”

结果将是:

/* No comment test. */
"Incoming calls" = "See the Preferences";

/* No comment test. */
"Incoming message from %@" = "Incoming message from %@";

/* No comment test. */
"Enter Your Password" = "";
以下是我的Python代码:

prefix = '"'
suffix = '";'
comment = '/* No comment test. */'




f = codecs.open('Strings.txt', encoding="utf-16")
o = codecs.open('temp.strings', 'w')

for line in f:
    o.write(line.replace(' = ', '\" = \"' ))
f.close()
o.close()

h = codecs.open('temp.strings', 'r')
t = codecs.open('Format.strings', 'w') 
for l in h:
    t.write(comment + '\n')
    t.write('%s%s%s\n' % (prefix, l.rstrip('\n') , suffix))
    t.write("\n");

t.close()
h.close()

有没有办法避免使用“临时字符串”文件(二次读取和写入)并获得相同的结果?

打开文件进行读取。然后替换字符。不要写入其他文件,而是将其写入Format.strings文件

prefix = '"'
suffix = '";'
comment = '/* No comment test. */'

f = codecs.open('Strings.txt',"r")
t = codecs.open('Format.strings', 'w+') 

for line in f.readlines():
    t.writelines(comment + '\n')
    line = line.replace(' = ', '\" = \"' )
    t.writelines('%s%s%s\n' % (prefix, line.rstrip('\n') , suffix))
    t.writelines("\n")
f.close()
t.close()
以Format.strings格式输出:

/* No comment test. */
"Incoming calls" = "See the Preferences";

/* No comment test. */
"Incoming message from %@" = "Incoming message from %@";

/* No comment test. */
"Enter Your Password" = "";

/* No comment test. */
"Dial Plans" = "Disable Calls";

/* No comment test. */
"Details not available" = "Call Button";

打开文件进行读取。然后替换字符。不要写入其他文件,而是将其写入Format.strings文件

prefix = '"'
suffix = '";'
comment = '/* No comment test. */'

f = codecs.open('Strings.txt',"r")
t = codecs.open('Format.strings', 'w+') 

for line in f.readlines():
    t.writelines(comment + '\n')
    line = line.replace(' = ', '\" = \"' )
    t.writelines('%s%s%s\n' % (prefix, line.rstrip('\n') , suffix))
    t.writelines("\n")
f.close()
t.close()
以Format.strings格式输出:

/* No comment test. */
"Incoming calls" = "See the Preferences";

/* No comment test. */
"Incoming message from %@" = "Incoming message from %@";

/* No comment test. */
"Enter Your Password" = "";

/* No comment test. */
"Dial Plans" = "Disable Calls";

/* No comment test. */
"Details not available" = "Call Button";

如果文件很小,则读取所有文件,并在内存中执行所有操作。如果不是,那就没有办法了,因为文件是以一种无法插入的方式存储的。@Am.rez谢谢,它们的大小将是动态的。。有时小,有时大。。感谢您在编写解决方案时将所有内容都放在内存中,但没有仔细阅读代码。。。输入和输出文件不同,因此您可以直接写入输出文件;o=codecs.EncodedFile(输出,“utf-16”)而不是临时文件如果文件很小,则读取所有文件,并在内存中执行所有操作。如果不是,那就没有办法了,因为文件是以一种无法插入的方式存储的。@Am.rez谢谢,它们的大小将是动态的。。有时小,有时大。。感谢您在编写解决方案时将所有内容都放在内存中,但没有仔细阅读代码。。。输入和输出文件不同,因此您可以直接写入输出文件;o=codecs.EncodedFile(输出,“utf-16”)而不是临时文件我看到您的代码的问题是,
t=codecs.open('Format.strings','w+'))中没有声明输出编码。
:它可以使用ASCII输入,但无法使用更高的字符。编解码器是@Benalison使用的。因此,我保留了相同的代码。我发现您的代码存在的问题是,
t=codecs.open('Format.strings','w+')
中没有声明输出编码:它可以使用ASCII输入,但无法使用更高的字符。codecs是@Benalison使用的。因此,我保留了同样的内容。