[英]How to save an edited txt file into a new txt file?
I am trying to save my output from x .txt
files in only one .txt
file. 我想我的输出从X保存
.txt
文件中只有一个.txt
文件。 The .txt
file should look like the output as you can see in the picture below . 如下面的图片所示 ,
.txt
文件应看起来像输出。 What this program actually does is read a couple of .txt
files with tons of data which I filter out using regex. 该程序的实际作用是读取几个带有大量数据的
.txt
文件,这些数据是我使用正则表达式过滤掉的。
My source code: 我的源代码:
import os,glob
import re
folder_path =(r"C:\Users\yokay\Desktop\DMS\Messdaten_DMT")
values_re = re.compile(r'\t\d+\t-?\d+,?\d*(\t-?\d+,?\d+){71}')
for filename in glob.glob(os.path.join(folder_path, '*.txt')):
with open(filename) as lines:
for line in lines:
match = values_re.search(line)
if match:
values = match.group(0).split('\t')
assert values[0] == ''
values = values[1:]
print(values)
Thank you for your time! 感谢您的时间! :)
:)
Then you just need to open a file and write values to it. 然后,您只需要打开一个文件并向其中写入值即可。 Try with this.
试试这个。 You might need to format (I cannot test since I don't have your text files. I am assuming the output you have in values is correct and keep in mind that this is appending, so if you run more than once you will get duplicates.
您可能需要设置格式(由于我没有文本文件,因此无法测试。我假设值中的输出正确,并且请记住,这是追加操作,因此,如果运行多次,则会得到重复项。
import os,glob
import re
folder_path =(r"C:\Users\yokay\Desktop\DMS\Messdaten_DMT")
values_re = re.compile(r'\t\d+\t-?\d+,?\d*(\t-?\d+,?\d+){71}')
outF = open("myOutFile.txt", "a")
for filename in glob.glob(os.path.join(folder_path, '*.txt')):
with open(filename) as lines:
for line in lines:
match = values_re.search(line)
if match:
values = match.group(0).split('\t')
assert values[0] == ''
values = values[1:]
outF.write(values)
print(values)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.