使用特定位置将文本从一个文件移动到另一个文件 python
Move text from one file to another using specific positions python
我想将文本文件的特定部分(使用特殊字符串,例如#)移动到另一个文件。喜欢:Similar Solution。我的问题是我还想在输出文件中指定目的地。
例如
我有一个文本文件,其中一段以#1 开头,一些行以#2 结尾
FIle1.txt
hello
a
b
#1
a
b
#2
c
我有另一个文件(不是 txt,而是 markdown),如下所示:
File2
header
a
b
#1
#2
d
我想将特殊字符(#1 和#2)内的文本从第一个文件移动到第二个文件中。结果:
File2
header
a
b
#1
a
b
#2
d
IIUC,你想要:
with open("file1.txt") as f:
f1 = [line.strip() for line in f]
with open("file2.md") as f:
f2 = [line.strip() for line in f]
output = f2[:f2.index("#1")]+f1[f1.index("#1"):f1.index("#2")]+f2[f2.index("#2"):]
with open("output.md", "w") as f:
f.write("\n".join(output))
我写了一个不太pythonic的例子,如果你有更多的锚点,它更容易修改。它使用了一个非常简单的状态机。
to_copy = ""
copy_state = False
with open("file1.txt") as f:
for el in f:
# update state. We need to leave copy state before finding end symbol
if copy_state and "#2" in el:
copy_state = False
if copy_state:
to_copy += el
# update state. We need to enter copy state after finding end symbol
if not copy_state and "#1" in el:
copy_state = True
#debug
print(to_copy)
output = ""
with open("file2.txt") as fc:
with open("file_merge.txt","w") as fm:
for el in fc:
#copy file 2 into file_merge
output += el
#pretty much the same logic
# update state. We need to leave copy state before finding end symbol
if copy_state and "#2" in el:
copy_state = False
# update state. We need to enter copy state after finding end symbol
if not copy_state and "#1" in el:
copy_state = True
# if detected start copy, attach what you want to copy from file1
output += to_copy
fm.write(output)
#debug
print(output)
我想将文本文件的特定部分(使用特殊字符串,例如#)移动到另一个文件。喜欢:Similar Solution。我的问题是我还想在输出文件中指定目的地。
例如
我有一个文本文件,其中一段以#1 开头,一些行以#2 结尾
FIle1.txt
hello
a
b
#1
a
b
#2
c
我有另一个文件(不是 txt,而是 markdown),如下所示:
File2
header
a
b
#1
#2
d
我想将特殊字符(#1 和#2)内的文本从第一个文件移动到第二个文件中。结果:
File2
header
a
b
#1
a
b
#2
d
IIUC,你想要:
with open("file1.txt") as f:
f1 = [line.strip() for line in f]
with open("file2.md") as f:
f2 = [line.strip() for line in f]
output = f2[:f2.index("#1")]+f1[f1.index("#1"):f1.index("#2")]+f2[f2.index("#2"):]
with open("output.md", "w") as f:
f.write("\n".join(output))
我写了一个不太pythonic的例子,如果你有更多的锚点,它更容易修改。它使用了一个非常简单的状态机。
to_copy = ""
copy_state = False
with open("file1.txt") as f:
for el in f:
# update state. We need to leave copy state before finding end symbol
if copy_state and "#2" in el:
copy_state = False
if copy_state:
to_copy += el
# update state. We need to enter copy state after finding end symbol
if not copy_state and "#1" in el:
copy_state = True
#debug
print(to_copy)
output = ""
with open("file2.txt") as fc:
with open("file_merge.txt","w") as fm:
for el in fc:
#copy file 2 into file_merge
output += el
#pretty much the same logic
# update state. We need to leave copy state before finding end symbol
if copy_state and "#2" in el:
copy_state = False
# update state. We need to enter copy state after finding end symbol
if not copy_state and "#1" in el:
copy_state = True
# if detected start copy, attach what you want to copy from file1
output += to_copy
fm.write(output)
#debug
print(output)