本文介绍了将复杂的txt转换为csv python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .txt 文件,其中包含此文件

I have a .txt file with this inside

Name: 321;
Score:100; Used Time: 1:09:308;
GTime: 6/28/2024 10:04:18 PM;
Core Version : 21.0.0.0;
Software Version : 21.0.0.0;
AppID: 0S0; MapDispName: Future City; MapName:MapName MapName MapName;
Key:A0000-abcde-Q0000-F0000-00H00;  REG Date : 2/27/2021 1:16:34 PM; Expiry : 7/7/2024 12:00:00 AM

我想做的是使用python脚本将该文本转换为 .csv (表).有300个文件,每个文件有数百行.我们只需要将前7行中的信息转换为csv.这300个文件全部具有相同的格式,但具有不同的值.

What I'm trying to do is convert that text into a .csv (table) using a python script.There are 300 files and hundreds of lines in each file. We only need to transform the information in the first 7 lines into csv. All of these 300 files have the same format but with different values.

我希望 log.csv 文件显示的是:

Name,Sore,Time,Software Ver,Core Ver,AppID,Key,REG Date,Expiry,MapName
321,100,69.308s,21.0.0.0,21.0.0.0,0S0,A0000-abcde-Q0000-F0000-00H00,2/27/2021 1:16:34 PM,7/7/2024 12:00:00 AM,MapName MapName MapName

如何使用python做到这一点?谢谢.

How can I do it with python? Thanks.

推荐答案

您的当前示例显示,所有值似乎都遵循相同的格式,即 Key:Value;

Your current example shows that all values appear to follow the same format i.e. Key:Value;

使用 glob.glob()遍历所有文本文件名.您可以使用 islice()精确读取7行,然后将它们转换为一行.然后可以在; 上对其进行拆分,以提供键值对的列表.然后可以在: strip()上进一步拆分,以删除多余的空格.

Use glob.glob() to iterate over all of your text filenames. You can use islice() to read in exactly 7 lines, then convert them into a single line. This can then be split on ; to give you a list of key value pairs. These can then be further split on the : and strip() applied to remove any extra spaces.

最后使用 itemgetter()从结果列表中仅提取所需的元素.

Lastly make use of itemgetter() to extract only the elements you need from the resulting list.

from itertools import islice, chain
from operator import itemgetter
import csv
import glob
import os

get = itemgetter(1, 3, 5, 9, 11, 13, 19, 21, 23, 17)

with open('log.csv', 'w', newline='') as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow('Name,Sore,Time,Software Ver,Core Ver,AppID,Key,REG Date,Expiry,Filename'.split(','))

    for filename in glob.glob('*.txt', recursive=True):
        with open(filename) as f_input:
            data = ''.join(islice(f_input, 0, 7)).replace('\n', '').split(';')
            values = [v.strip() for v in chain.from_iterable(d.split(':', 1) for d in data)]
            csv_output.writerow([*get(values), os.path.basename(filename)])

以您的示例为例,这将为您提供包含以下内容的 log.csv :

For your example, this would give you log.csv containing:

Name,Sore,Time,Software Ver,Core Ver,AppID,Key,REG Date,Expiry,Filename
321,100,1:09:308,21.0.0.0,21.0.0.0,0S0,A0000-abcde-Q0000-F0000-00H00,2/27/2021 1:16:34 PM,7/7/2024 12:00:00 AM,MapName MapName MapName,file1.txt

这篇关于将复杂的txt转换为csv python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 11:14