Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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_Python 3.x_Python 2.7_Concatenation_String Concatenation - Fatal编程技术网

如何将字符串值与文本文件Python中的每一行连接起来

如何将字符串值与文本文件Python中的每一行连接起来,python,python-3.x,python-2.7,concatenation,string-concatenation,Python,Python 3.x,Python 2.7,Concatenation,String Concatenation,我有一个大的文本文件,它包含700k行。 我想在每一行中串联或附加小字符串“/products/all.atom” 我试过这个密码 enter code here import os import sys import fileinput print ("Text to search for:") textToSearch = input( "> " ) print ("Text to replace it with:") textToReplace = input( "> "

我有一个大的文本文件,它包含700k行。 我想在每一行中串联或附加小字符串“/products/all.atom”

我试过这个密码

enter code here
import os
import sys
import fileinput

print ("Text to search for:")
textToSearch = input( "> " ) 

print ("Text to replace it with:")
textToReplace = input( "> " )

print ("File to perform Search-Replace on:")
fileToSearch  = input( "> " )
#fileToSearch = 'D:\dummy1.txt'

tempFile = open( fileToSearch, 'r+' )

for line in fileinput.input( fileToSearch ):
if textToSearch in line :
    print('Match Found')
else:
    print('Match Not Found!!')
tempFile.write( line.replace( textToSearch, textToReplace ) )
tempFile.close()


input( '\n\n Press Enter to exit...' )
但代码并不是完美的,我在这里做的是用.com/products/all.atom替换“.com”,但是当我运行这个命令时,循环变得无限大,它写入的文件大小为10gb

下面是我想要的示例:

store1.com > store1.com/products/all.atom
store2.com > store2.com/products/all.atom
store3.com > store3.com/products/all.atom


请帮助我。

尝试列表理解和字符串连接:

fo=open('sta.txt','r+') # open file for read    
lines=fo.read() # reading lines in file 
fo.close()  
fo=open('sta.txt','w') # open file for Write    
for i in lines.split('\n')[:-1]:  #split the lines (using \n)and avoid last one
    fo.write(i+' hai \n') # write new lines #replace 'hai with what you need to append
fo.close()
with open('file.txt','r') as f:
    print([line.strip() + "/products/all.atom" for line in f])
输出:

['store1.com/products/all.atom', 'store2.com/products/all.atom', 'store3.com/products/all.atom', 'store4.com/products/all.atom']
更新的解决方案:

以下是您将如何在新文件中写入:

with open('names','r') as f:
    data=[line.strip() + "/products/all.atom" for line in f]
    with open('new_file','w+') as write_f:
        for line_1 in data:
            write_f.write(line_1 + '\n')

转换是直接进行的,只需对from和to字符串执行
str.replace
。写入临时文件,以便在您仍在处理时不会覆盖该文件,并在完成后重命名该文件

file.writelines()
方法使用迭代器并重复调用它以获取要写入的行。我使用了一个生成器来读取文件的每一行并替换文本

import os

print ("Text to search for:")
textToSearch = input( "> " ) 

print ("Text to replace it with:")
textToReplace = input( "> " )

print ("File to perform Search-Replace on:")
fileToSearch  = input( "> " )

outName = fileToSearch + ".tmp"

with open(fileToSearch) as infile, open(outName, 'w') as outfile:
    outfile.writelines(line.replace(textToSearch, textToReplace) for line in infile)

print("Lines replaced, do you want to overwrite original file? (y/n):")
replace = input("> ")
if replace.startswith ('y'):
    os.remove(fileToSearch)
    os.rename(outName, fileToSearch)

input( '\n\n Press Enter to exit...' )

在你向我们展示了你解决问题的尝试之后,我们倾向于加入进来。这应该是一个简短的脚本,你可以张贴在这里。包括几行示例数据,以便我们可以看到这些行的外观。请立即检查。@SyedAashir您已包括示例数据,但未包含您试图解决问题的示例。你试过什么方法?你在哪里卡住了?提供一个最小的代码示例,以显示您正在执行的操作trying@tdelaney请现在检查。@daveruinseverything立即检查。它的工作文件是打印,但现在我如何将输出保存在文本文件中。