循环遍历 .txt 以根据数据索引创建两个分隔符
Loop through .txt to create two delimiters depending on data index
所以我有许多文件(1000 左右),每个文件都有 90k 多行,其中数据以不正确的格式记录,我正在尝试重新格式化 txt 文件。
目前的数据是这样的:
9/3/2021 00 00 00 -0.18 -2.40 -2.40
9/3/2021 00 00 01 -0.18 -2.40 -2.40
9/3/2021 00 00 02 -0.18 -2.40 -2.40
9/3/2021 00 00 03 -0.17 -2.40 -2.40
.....
但是,它应该看起来像
9/3/2021,00:00:00,-0.18,-2.40,-2.40
9/3/2021,00:00:01,-0.18,-2.40,-2.40
9/3/2021,00:00:02,-0.18,-2.40,-2.40
9/3/2021,00:00:03,-0.17,-2.40,-2.40
....
我已经设法检查了所有内容并添加了一个“,”
input = open(os.path.expanduser("~/Desktop/ssdat/SegY TidalCorrection 03092021.txt"))
content = input.read()
content = content.replace(" ", ",")
print(content)
导致输出:
9/3/2021,17,22,47,0.20,1511.10,-2.12,-2.29
9/3/2021,17,22,48,0.01,1511.10,-2.29,-2.29
9/3/2021,17,22,49,-0.17,1511.05,-2.41,-2.29
9/3/2021,17,22,50,-0.14,1511.02,-2.34,-2.30
所以我想我的问题是如何遍历所有内容以创建两个新的分隔符?
一种选择是逐行遍历文本文件,然后逐字符遍历。假设您已经知道应该如何格式化数据,那么您可以查找空格并将其替换为所需的分隔符。
类似于@bret-hogg 的回答。但是,如果您只对在每一行中重新创建一个简单的字符串感兴趣,那么以下方法将起作用:
import os
with open("your_old_file.txt", "r") as text_file, open("your_new_file.txt", "w") as csv_file:
for line in text_file:
fields = line.split(" ")
date = fields[0]
time = fields[1] + ":" + fields[2] + ":" + fields[3]
replacement_row = f"{date},{time},{fields[4]},,,{fields[5]},{fields[6]}"
csv_file.write(replacement_row)
所以我有许多文件(1000 左右),每个文件都有 90k 多行,其中数据以不正确的格式记录,我正在尝试重新格式化 txt 文件。
目前的数据是这样的:
9/3/2021 00 00 00 -0.18 -2.40 -2.40
9/3/2021 00 00 01 -0.18 -2.40 -2.40
9/3/2021 00 00 02 -0.18 -2.40 -2.40
9/3/2021 00 00 03 -0.17 -2.40 -2.40
.....
但是,它应该看起来像
9/3/2021,00:00:00,-0.18,-2.40,-2.40
9/3/2021,00:00:01,-0.18,-2.40,-2.40
9/3/2021,00:00:02,-0.18,-2.40,-2.40
9/3/2021,00:00:03,-0.17,-2.40,-2.40
....
我已经设法检查了所有内容并添加了一个“,”
input = open(os.path.expanduser("~/Desktop/ssdat/SegY TidalCorrection 03092021.txt"))
content = input.read()
content = content.replace(" ", ",")
print(content)
导致输出:
9/3/2021,17,22,47,0.20,1511.10,-2.12,-2.29
9/3/2021,17,22,48,0.01,1511.10,-2.29,-2.29
9/3/2021,17,22,49,-0.17,1511.05,-2.41,-2.29
9/3/2021,17,22,50,-0.14,1511.02,-2.34,-2.30
所以我想我的问题是如何遍历所有内容以创建两个新的分隔符?
一种选择是逐行遍历文本文件,然后逐字符遍历。假设您已经知道应该如何格式化数据,那么您可以查找空格并将其替换为所需的分隔符。
类似于@bret-hogg 的回答。但是,如果您只对在每一行中重新创建一个简单的字符串感兴趣,那么以下方法将起作用:
import os
with open("your_old_file.txt", "r") as text_file, open("your_new_file.txt", "w") as csv_file:
for line in text_file:
fields = line.split(" ")
date = fields[0]
time = fields[1] + ":" + fields[2] + ":" + fields[3]
replacement_row = f"{date},{time},{fields[4]},,,{fields[5]},{fields[6]}"
csv_file.write(replacement_row)