我是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)|发布于 2021-04-16 20:46:36
希望这对你有用。它获取一个文件路径,读取并转换数据,然后使用第二个函数将其写出
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)发布于 2021-04-16 21:09:59
替换以下代码中的文件路径。
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)https://stackoverflow.com/questions/67125066
复制相似问题