将文本文件中的某些数据导出到CSV文件,而文本文件具有不同类型的分隔符

2024-04-20 03:19:40 发布

您现在位置:Python中文网/ 问答频道 /正文

TT1  4444 | Drowsy | 9 19   bit drowsy
TT2  45888 | Blurred see - hazy | 29 50 little seeing vision
TT4  45933 | Excessive upper pain  | 62 78  pain problems

我想将部分信息导出到excel表或CSV文件。我期望的CSV文件如下:

Column 1    Column 2                      column 3
4444        Drowsy                        bit drowsy 
45888       Blurred see - hazy            little seeing vision
45933       Excessive upper pain          pain problems

如您所见,我不需要文本文件的第一、第四和第五列中的信息。你知道吗

问题更新: 某些行中的信息结构如下:

TT6 112397013 | ari | or 76948002|pain| 22 345  agony

预期产出如下:

Column 1    Column 2                      column 3
112397013     air                          agony
76948002      pain                         agony

问题的第二次更新:文本文件中存在另一个异常:

TT9 CONCEPT_LESS 336 344    mobility

我只希望这行的输出是这样的:

CONCEPT_LESS   mobility

有什么建议吗?谢谢!你知道吗


Tags: bitcolumnupperproblemsseevisionpainlittle
2条回答

我假设你可以读取字符串列表中的数据。代码使用正则表达式(re)将它们解析为所需的输出,然后可以将其写入csv文件:

import re

#read lines from file using:
#lines = my_file.readlines()
lines = ["TT1  4444 | Drowsy | 9 19   bit drowsy",
         "TT2  45888 | Blurred see - hazy | 29 50 little seeing vision",
         "TT4  45933 | Excessive upper pain  | 62 78  pain problems"]

#Looks for TT some whitespace then numbers until another whitespace and vertical bar
tt_num_pattern = "TT.*\s([0-9].*?)\s"

#Only looks for letters after a space
describe_pattern = "\s(\D.*)"

#Format the output lines
out_lines = []
for line in lines:
    split_line = line.split("|")
    tt_num = re.findall(tt_num_pattern,split_line[0])[0]

    state = split_line[1].strip() #Just trim edges of whitespace
    describe = re.findall(describe_pattern,split_line[2])[0]
    describe = describe.strip()

    out_line = tt_num+","+state+","+describe
    out_lines.append(out_line)

#Print them out (would normally want to write to file after header line)
for out_line in out_lines:
    print out_line

输出:

4444,Drowsy,bit drowsy
45888,Blurred see - hazy,little seeing vision
45933,Excessive upper pain,pain problems

很高兴这有帮助。这是你要求的更新。老实说,这不是很好(灵活)的代码,但它的工作:

import re

#read lines from file using:
#lines = my_file.readlines()
lines = ["TT1  4444 | Drowsy | 9 19   bit drowsy",
         "TT2  45888 | Blurred see - hazy | 29 50 little seeing vision",
         "TT4  45933 | Excessive upper pain  | 62 78  pain problems",
         "TT6 112397013 | air | or 76948002|pain| 22 345  agony"]

#Looks for TT some whitespace then numbers until another whitespace and vertical bar
tt_num_pattern = "TT.*\s([0-9].*?)\s"

#Only looks for letters after a space
describe_pattern = "\s(\D.*)"

#Format the output lines
out_lines = []
for line in lines:

    split_line = line.split("|")

    #If there is an 'or'
    if len(split_line) == 5:
        tt_num = split_line[2].replace("or","").strip()
        state = split_line[3].strip()
        describe = re.findall(describe_pattern,split_line[4])[0].strip()
        out_line = tt_num+","+state+","+describe
        out_lines.append(out_line)

        tt_num = re.findall(tt_num_pattern,split_line[0])[0]
        state = split_line[1].strip()
        out_line = tt_num+","+state+","+describe
        out_lines.append(out_line)


    #If there is no 'or'
    elif len(split_line) == 3:
        tt_num = re.findall(tt_num_pattern,split_line[0])[0]

        state = split_line[1].strip() #Just trim edges of whitespace
        describe = re.findall(describe_pattern,split_line[2])[0]
        describe = describe.strip()

        out_line = tt_num+","+state+","+describe
        out_lines.append(out_line)

#Print them out (would normally want to write to file after header line)
for out_line in out_lines:
    print out_line

更新的输出:

4444,Drowsy,bit drowsy
45888,Blurred see - hazy,little seeing vision
45933,Excessive upper pain,pain problems
76948002,pain,agony
112397013,air,agony

由于输入文本文件没有特定类型的分隔符(管道、空格或逗号),我们需要将文件作为字符串读取。你知道吗

为了提取所需的信息,使用regex。你知道吗

csv模块用于创建数据并将数据写入csv。你知道吗

有关csv模块的更多信息,请check here。你知道吗

内容xyz.txt文件

TT1  4444 | Drowsy | 9 19   bit drowsy
TT2  45888 | Blurred see - hazy | 29 50 little seeing vision
TT4  45933 | Excessive upper pain  | 62 78  pain problems
TT6 112397013 | air | or 76948002|pain| 22 345  agony
TT9 CONCEPT_LESS 336 344    mobility

代码(内联注释):

import re
import csv


def extract_data(val):
    tmp1,tmp2,tmp3 = val[0],val[1],val[2]
    tmp1 = re.findall(r'.*\s+(\w+)',tmp1.strip())[0]
    tmp2 = tmp2.strip()
    tmp3 = re.findall(r'\s+(\D+)',tmp3.strip())[0]
    return (tmp1,tmp2,tmp3)

#Open CSV file for wrting data
csv_fh = open("demo.csv", 'w')
writer = csv.writer(csv_fh)
#Write Header to csv file
writer.writerow( ('Column 1', 'Column 2', 'Column 3') )

#Start reading text file line by line
with open("xyz.txt","r") as fh:
    for line in fh.readlines():
        #Check or in line
        if "or" in line:
            val_list = line.split('|')
            val1 = val_list[:2]
            val2 = val_list[2:]
            val1.append(val2[-1])
            for v in [val1,val2]:
                l = extract_data(v)
                writer.writerow( l )
        elif '|' in line and 'or' not in line:
            #Split on basis of pipe(|)
            val = line.split('|')
            l = extract_data(val)
            writer.writerow( l )
        elif '|' not in line:
            val = line.split()
            data = [val[1],val[4],'']
            writer.writerow( data )
        else:
            pass

#Close CSV file
csv_fh.close()

内容演示.csv:

Column 1,Column 2,Column 3
4444,Drowsy,bit drowsy
45888,Blurred see - hazy,little seeing vision
45933,Excessive upper pain,pain problems
112397013,air,agony
76948002,pain,agony
CONCEPT_LESS,mobility,

相关问题 更多 >