首页 > 解决方案 > 如何清理我的数据,以便将其转换为 csv 文件

问题描述

我有一个如下所示的文本文件。我的问题是,有没有一种方法可以清理数据而无需手动执行,以便我可以将其转换为像 csv 这样的数据格式?

    Word, Hiragana, English
    会う, あう -to meet
    青, あお -blue
    青い, あおい -blue
    赤, あか -red
    赤い, あかい -red
    明い, あかるい -bright
    秋, あき -autumn
    開く, あく -to open,to become open
    開ける, あける -to open
    上げる, あげる -to give
    朝, あさ -morning
    朝御飯, あさごはん -breakfast
    あさって -day after tomorrow
    足, あし -foot,leg
    明日, あした -tomorrow
    あそこ -over there
    遊ぶ, あそぶ -to play,to make a visit
    暖かい, あたたかい -warm
    頭, あたま -head
    新しい, あたらしい -new
    あちら -there
    暑い, あつい -hot
    熱い, あつい -hot to the touch
    厚い, あつい -kind, deep, thick
    あっち -over there

标签: pythonpandas

解决方案


这样的事情应该适用于您的情况:

for l in textfile:
    split1 = l.split(',', 1) # split on first occurance  of ',' in line

    word = split1[0] # take the first argument of the split as word

    split2 = split1[0].split('-', 1) # split the second part with first occurance of '-'
    hiragana = split2[0]
    english = split2[1]

推荐阅读