gpt4 book ai didi

python - 将txt文件解析为2个csv文件

转载 作者:行者123 更新时间:2023-12-01 03:52:06 25 4
gpt4 key购买 nike

嗨,我已经有了使用这种模式解析 *.txt 的工作代码:

...
0.00001
0.00280
0.00022
...

进入*.csv文件

我这样做:

in_txt = csv.reader(open(txt_file, "rb"), delimiter = '\n')
f = open(csv_file, 'wb')
out_csv = csv.writer(f)
out_csv.writerows(in_txt)
f.close()

我需要帮助修改它以便能够解析以下模式:

...
0.00001@0.02234
0.00280@0.00001
0.00022@0.03992
...

分成 2 个 *.csv 文件(第一个包含第一个“列”,第二个包含第二个“列”)

最佳答案

经过测试的示例:

import csv, os

txt_file = '/path/to/in.txt'
in_txt = csv.reader(open(txt_file, 'rb'), delimiter='@')
out_file1 = '/path/to/out1.txt'
out_file2 = '/path/to/out2.txt'
with open(out_file1, 'wb') as fou1, open(out_file2, 'wb') as fou2:
for one, two in in_txt:
fou1.write(one + os.linesep)
fou2.write(two + os.linesep)

关于python - 将txt文件解析为2个csv文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38062733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com