拆分文本行并创建变量

2024-04-29 16:06:50 发布

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

我是python的初学者,我尝试使用它来自动化重复的任务。我正在努力完成以下任务:我有一大堆文本文件,格式如下: 如何从以下文本行中删除:

Cluster -44 -58 +36  :
248 voxels (69%) covering 5% of atlas.sLOC l (Lateral Occipital Cortex, superior division Left)
51 voxels (14%) covering 5% of atlas.AG l (Angular Gyrus Left)
62 voxels (17%) covering 0% of atlas.not-labeled

Cluster -38 -84 +18  :
163 voxels (47%) covering 3% of atlas.sLOC l (Lateral Occipital Cortex, superior division Left)
49 voxels (14%) covering 0% of atlas.not-labeled

Cluster -42 -6 +30  :
89 voxels (34%) covering 2% of atlas.PreCG l (Precentral Gyrus Left)
1 voxels (0%) covering 0% of atlas.IFG oper l (Inferior Frontal Gyrus, pars opercularis Left)

创建以下数据帧:

|x|y|z|voxels|voxels_pct|covering_pct|atlas|
|-|-|-|------|----------|------------|-----|
|-44 |-58 |+36|248|69|5|sLOC l (Lateral Occipital Cortex, superior division Left)|
|-44 |-58 |+36|51|14|5|AG l (Angular Gyrus Left)|
|-44 |-58 |+36|62|17|0|not-labeled|
|-38 |-84 |+18|163|34|3|sLOC l (Lateral Occipital Cortex, superior division Left)|
|-38 |-84 |+18|49|14|0|not-labeled|
|-42 |-6 |+30|89|34|2|PreCG l (Precentral Gyrus Left)|
|-42 |-6 |+30|1|0|0|IFG oper l (Inferior Frontal Gyrus, pars opercularis Left)|

Tags: ofcortexnotleftdivisionclusteratlaslabeled
2条回答

替换以下代码中的文件路径

df=[]
d={}
filepath='test.txt'
with open(filepath) as test:
    fp=test.readlines()
    for i in fp:
        if i.find('Cluster')!=-1:
            d['x']=i.split()[1]
            d['y']=i.split()[2]
            d['z']=i.split()[3]
        else:
            if len(i)<=1:
                continue
            else:
                d['voxels']=i.split()[0]
                d['voxels_pct']=i.split()[2][1:-2]
                d['covering_pct']=i.split()[4][:-1]
                d['atlas']=i[i.find('atlas.'):-2]
                df.append(d.copy())
pd.DataFrame(df)

希望这对你有用。它获取文件路径,读取和转换数据,然后使用第二个函数将其写出

from pathlib import Path
import csv
import re

def read_cluster_file(path):
    pat = re.compile(r'(\d+) voxels \((\d+)%\) covering (\d+)')
    p = Path(path)
    
    READING = False
    data = []

    for line in p.read():
        if line.startswith('Cluster'):
            # begin parsing the data
            READING = True
            x, y, z = map(int, line.split()[1:4])
            continue

        if READING and not line.strip():
            # end of data row; reset x, y, z
            READING = False

        if READING:
           try:
               v, vp, cp = map(int, pat.findall(s)[0])
               atlas = line.split('.', 1)[1]
           expect IndexError:
               print(f'With x, y, z = {x}, {y}, {z}')
               print('Could not parse line: ' + line)
               data.append(['Parse error', , , , , , ])
               continue
           row = [x, y, z, v, vp, cp, atlas]
           data.append(row)
    return data

def write_data(path, data):
    p = Path(path).with_suffix('.csv')
    with p.open('w') as fp:
        writer = csv.writer(fp)
        writer.writerows(data)

使用是:

path = 'C:/path/to/some/file.txt'
data = read_cluster_file(path)
write_data(path, data)

相关问题 更多 >