タイムスタンプ付きのトランスクリプトであるテキストファイルがあります。次のようになります。
00:25
hold it miles lunch and remember I'm
00:30
working late tonight again man you're a
00:34
total slave to that business of yours
00:36
nobody's a slave to their own dream
私はそれをパンダデータフレームにインポートする方法を理解しようとしているので、次のようになります:
[Time] [Text]
00:25 hold it miles lunch and remember I'm
00:30 working late tonight again man you're a
00:34 total slave to that business of yours
00:36 nobody's a slave to their own dream
どこから始めればいいのかさえわからないと言うのは恥ずかしいです...私が知っていて試したすべての方法がこれを生み出します:
row1 00:25
row2 hold it miles lunch and remember I'm
row3 00:30
row4 working late tonight again man you're a
row5 00:34
row6 total slave to that business of yours
row7 00:36
row8 nobody's a slave to their own dream
この質問を見つけました。同じ問題のようですが、データフレームを作成するときにどのように適用するかわかりません。
助けてくれてありがとう!
すべての行を分割し、1行おきに別の列(TimeやTextなど)に割り当てることで、これを行う別の方法。最後に、変更した辞書からDataFrameにします。
import pandas as pd
# Read your files here
files = ['text.txt'] # you can add file or bunch of files
data = {}
for f in files:
with open (f, "r") as myfile:
all_lines = myfile.read().splitlines() # split by line
# assign every alternative line to Time and Text index alternatively
data['Time'], data['Text'] = all_lines[::2], all_lines[1::2]
# create dataframe from the dictionary
df = pd.DataFrame(data)
print(df)
出力:
Time Text
0 00:25 hold it miles lunch and remember I'm
1 00:30 working late tonight again man you're a
2 00:34 total slave to that business of yours
3 00:36 nobody's a slave to their own dream
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加