如何将字符串值与文本文件中的每一行连接 Python
How to concatenation string value with every line in text file Python
我有一个包含 70 万行的大型文本文件。
我想在每一行中连接或附加小字符串“/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
click here to check text file
请帮助我。
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...' )
我有一个包含 70 万行的大型文本文件。 我想在每一行中连接或附加小字符串“/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
click here to check text file
请帮助我。
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...' )