使用逗號分隔的值拆分csv中的行

我有一個csv文件,列中的信息(id和文本)如下面的示例所示:

1, ?ildomos grindys
2, ?ildomos grindys, Rekuperacin? sistema
3, 
4, Skalbimo ma?ina, Su baldais, ?aldytuvas, ?ildomos grindys

我想要的輸出是將ID傳輸?shù)揭恍校⑵渑c其文本關(guān)聯(lián)(用于數(shù)據(jù)庫)。由于csv文件非常大,我只給你一小部分來了解我想要什么:

| ID             | Features   
+----------------+-------------
| 1              | ?ildomos grindys
| 2              | ?ildomos grindys
| 2              | Rekuperacin? sistema
| 3              | null
| 4              | Skalbimo ma?ina
| 4              | Su baldais
| 4              | ?aldytuvas
| 4              | ?ildomos grindys

我如何通過python做到這一點?謝謝

? 最佳回答:

這里有一種方法可以滿足您的要求:

with open('infoo.txt', 'r', encoding="utf-8") as f:
    records = []
    rows = [[x.strip() for x in row.split(',')] for row in f.readlines()]
    for row in rows:
        for i in range(1, len(row)):
            records.append([row[0], row[i] if row[i] else 'null'])
    with open('outfoo.txt', 'w', encoding="utf-8") as g:
        g.write('ID,Features\n')
        for record in records:
            g.write(f'{",".join(field for field in record)}\n')

# check the output file:
with open('outfoo.txt', 'r', encoding="utf-8") as f:
    print('contents of output file:')
    [print(row.strip('\n')) for row in f.readlines()]

Output:

contents of output file:
ID,Features
1,?ildomos grindys
2,?ildomos grindys
2,Rekuperacin? sistema
3,null
4,Skalbimo ma?ina
4,Su baldais
4,?aldytuvas
4,?ildomos grindys

UPDATE:

另一種方法是使用pandas(docs)。Pandas提供了許多處理表格數(shù)據(jù)的強大方法,但它也有一點學習曲線:

import pandas as pd
with open('infoo.txt', 'r', encoding="utf-8") as f:
    records = []
    rows = [[x.strip() for x in row.split(',')] for row in f.readlines()]
    df = pd.DataFrame([[row[0], row[1:]] for row in rows], columns=['ID', 'Feature'])
    print('Dataframe read from input file:'); print(df)
    df = df.explode('Feature').reset_index(drop=True)
    print('Dataframe with one Feature per row:'); print(df)
    df.to_csv('outfoo.txt', index = False)

    # check the output file:
    df2 = pd.read_csv('outfoo.txt')
    print('Dataframe re-read from output file:'); print(df2)

Output

Dataframe read from input file:
  ID                                            Feature
0  1                                 [?ildomos grindys]
1  2           [?ildomos grindys, Rekuperacin? sistema]
2  3                                                 []
3  4  [Skalbimo ma?ina, Su baldais, ?aldytuvas, ?ild...
Dataframe with one Feature per row:
  ID               Feature
0  1      ?ildomos grindys
1  2      ?ildomos grindys
2  2  Rekuperacin? sistema
3  3
4  4       Skalbimo ma?ina
5  4            Su baldais
6  4            ?aldytuvas
7  4      ?ildomos grindys
Dataframe re-read from output file:
   ID               Feature
0   1      ?ildomos grindys
1   2      ?ildomos grindys
2   2  Rekuperacin? sistema
3   3                   NaN
4   4       Skalbimo ma?ina
5   4            Su baldais
6   4            ?aldytuvas
7   4      ?ildomos grindys

pandas的文檔鏈接如下:

主站蜘蛛池模板: 在线视频一区二区三区三区不卡 | 视频一区视频二区在线观看| 高清在线一区二区| 中文人妻无码一区二区三区| 日韩一区二区三区视频久久| 国模私拍福利一区二区| 一区二区视频在线观看| av无码一区二区三区| 精品理论片一区二区三区| 夜精品a一区二区三区| 亚洲国产成人久久一区二区三区 | 日本美女一区二区三区 | 免费看一区二区三区四区| 无码精品前田一区二区| 国产一区二区三区免费看| 国产AV天堂无码一区二区三区| 色天使亚洲综合一区二区| 中文字幕一区在线观看视频| 日本片免费观看一区二区| 亚洲AV无码国产一区二区三区| 日韩精品一区二区三区老鸭窝 | 亚洲香蕉久久一区二区| 一区二区传媒有限公司| 欲色aV无码一区二区人妻| 成人毛片一区二区| 精品一区二区三区无码视频| 精品91一区二区三区| 成人国内精品久久久久一区| 日韩综合无码一区二区| 无码少妇一区二区性色AV| 国产日本一区二区三区| 人妻夜夜爽天天爽一区| 亚洲欧洲一区二区三区| 国产午夜精品一区二区三区漫画| 国产在线一区二区三区| 亚洲一区二区三区四区视频| 亚洲变态另类一区二区三区| 国产AV一区二区精品凹凸 | 日本高清一区二区三区| 国产一区二区三区在线电影| 狠狠做深爱婷婷综合一区 |