简体   繁体   English

如何使用python复制一个文件中的行并将其写入另一文件中?

[英]How to copy lines from one file and write them in other file using python?

I have sequence of data as shown below: 我有如下所示的数据序列:

XXXXXXX
DATA = 1,2,3,4,5,6,
       7,8,9,10,11,12,14,
       15 
XXXXXXX

if there is continuation line ends with "," else it has no comma",". 如果有延续行以“,”结尾,则没有逗号“,”。

How can I copy only DATA from given file and write it in other file? 如何仅从给定文件复制DATA并将其写入其他文件? Please help me! 请帮我!

You can do something like this: 您可以执行以下操作:

import re

data_file = open("data.txt", 'r')
data = data_file.read()

search = re.search("DATA\s\=\s((.*\d.+\,?\n)+)", data)
print search.group(1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用python将特定行从一个文件写入另一个文件 - Using python to write specific lines from one file to another file Python 如何在一个 file.txt 中写入 5 行,在一个新的 file1.txt 中写入另外 3 行,例如 +=1 - Python How to write 5 lines in one file.txt and the other 3 lines in a new file1.txt with +=1 for example 使用 Python 将行数复制 n 次并将它们写入其他文件 - Copy number of rows for n number of times using Python and write them in other file Python,读取有多少行,然后每行取一个文件并用另一个文件中的数字减去它们 - Python, read how many lines there are, then take of one file every line and substract them with the number in the other file 如何将一个文件的最后50行写入另一个Python - How to write last 50 lines from one file to another Python 如何使用python将所需的行从一个csv文件复制到另一个csv文件? - How do I copy only the required rows from one csv file to other csv file using python? 从文件一次复制x行,然后将其粘贴到Python的(^ v)中 - Copy x lines at a time from a file, then paste them in (^v) in Python 如何使用Python中的函数搜索文件中的特定行并将其写入另一个文件 - How to search specific lines in a file and write them to another file, using function in Python 将行从一个文本文件复制到另一个文本文件直到找到匹配文本的 Python 代码 - Python code to copy lines from one text file to other till matching text is found 如何复制不同 JSON 文件的正文并将它们全部写入一个带有 Python 的文件? - How can I copy the body of different JSON files and write them all to one file with Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM