简体   繁体   中英

split the csv into parts based on keywords in a row and then transpose it

I have a csv file that look like this:

read, part1,,,
BL, WL, layer, 23, 56
BL, WL, layer, 67, 92
read, part2,,,
BL, WL, layer, 29, 53
BL, WL, layer, 10, 79
read, part3,,,
BL, WL, layer, 56, 93
BL, WL, layer, 10, 38
.......

I want to separate this csv to 3 parts starting with keyword 'read part' and then transpose them into something like this:

read, part 1,,,              read, part 2,,,               read, part 3,,,
BL, WL, layer, 23, 56     BL, WL, layer, 29, 53,     BL, WL, layer, 56, 93
BL, WL, layer, 67, 92     BL, WL, layer, 10, 79,     BL, WL, layer, 10, 38
.......

anyone has idea how to achieve it? any idea is appreciated, thanks!

From your example, it seems you want it all in one file, I hope this helps:

import pandas as pd
import csv

result = pd.DataFrame()

with open('file.csv', 'r') as f:
    tables = []
    for line in f.readlines():
        if 'part' in line:
            part = line.split(',')[1].split('part')[1]
            tables.append([line.strip()])
        else:
            tables[int(part) - 1].append(line.strip())

    for table in tables:
        result = pd.concat([result, pd.DataFrame(table)], axis=1)

    headers = result.iloc[0]
    result.columns = headers
    result.drop(0, inplace=True)

print(result)

Make sure to change the file name in the with open() statement.

This results in a pandas DataFrame with the following contents:

0         read, part1,,,         read, part2,,,         read, part3,,,
1  BL, WL, layer, 23, 56  BL, WL, layer, 29, 53  BL, WL, layer, 56, 93
2  BL, WL, layer, 67, 92  BL, WL, layer, 10, 79  BL, WL, layer, 10, 38

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM