我有包含機器學習資料的 .txt 檔案。每個檔案都有 19 個條目,這些條目在文本檔案中一次又一次地重復。我需要以這樣的方式在 python 中獲取這些檔案,即每個條目都成為一列,并且它們相應的數值被放置在該列中。我需要使用 txt 檔案中設定的所有 19 個條目(如下面的“1”)來執行此操作。或者只包含數字資料的文本檔案也可以(如下面的“2”)。如果您可以在 python 中為此撰寫代碼,那將會很棒,否則請描述一些細節如何做到這一點
我想要的輸出:
1點擊查看圖片 或 2點擊查看圖片
文本檔案:
frame.id: 263126,Timestamp: 697287019071, Hand_number: 1 ,hand_Id_type: 238right hand ,hand_finger's_number:2,hand direction: (-0.142081, 0.865413, -0.4805413, -0.4805423, -0.4805423, -0.4805223, 48042823Palm)正常:(-0.686854,-0.435733,-0.581694),Finger_type(tipposition)TYPE_THUMB(-36.7239,301.602,330.845),Finger_type(tipposition)TYPE_INDEX(-14.9321,347.039,280.375),Finger_type(tipposition)TYPE_MIDDLE(5.5661,258.191 ,321.318),Finger_type(tipposition)TYPE_RING(20.0886,251.219,320.136),Finger_type(tipposition)TYPE_PINKY(27.5919,259.584,310.508)Frame.id:263127,時間戳:697287037765,Hand_number:1,hand_Id_type:238right手,hand_finger” s_number:2,手方向: (-0.167599, 0.827034, -0.536587) ,手掌位置: (31.7441, 283.449, 322.619) ,手掌正常: (-0.776892, -0.444.8)Finger_type(tipposition)TYPE_THUMB(-38.8083,304.444,330.446),Finger_type(tipposition)TYPE_INDEX(-22.0532,344.008,273.496),Finger_type(tipposition)TYPE_MIDDLE(-0.161068,258.09,319.489),Finger_type(tipposition)TYPE_RING(13.8233, 250.236, 317.138) ,Finger_type(tipposition) TYPE_PINKY(20.1892, 257.352, 305.739) Frame.id: 263128,Timestamp: 697287057570,Hand_type:10-hand_type,7hand_number:10hand_number1,7hand_number:105.739_hand_number:105.739 , -0.547284) ,掌心位置: (30.8754, 280.871, 315.444) ,掌心正常: (-0.750039, -0.473481, -0.461797) ,Finger_type(tipposition),90.40.40.40.40.310.400.45.45.45.4445.4445.44445.444 -23.5209, 339.286, 264.435) ,Finger_type(tipposition) TYPE_MIDDLE(-0.157164, 254.483, 311.627) ,Finger_type(tipposition) TYPE_RING(14.177206)Finger_type(tipposition) TYPE_PINKY(20.45, 254.358, 298.274) Frame.id: 263129,Timestamp: 697287076710, Hand_number: 1 ,hand_Id_type: 238right hand,hand_finger,109,103-109-number.109-103-103-109-number 方向手掌位置:(28.6877, 277.055, 299.705) ,手掌正常: (-0.739979, -0.472093, -0.479124) ,Finger_type(tipposition) TYPE_THUMB(-42.98438,type.555129), 3.550.39.559F TYPE_THUMB(-42.9848.5555129) 250.27) ,Finger_type(tipposition) TYPE_MIDDLE(-1.90294, 250.26, 294.934) ,Finger_type(tipposition) TYPE_RING(12.5955, 243.157, 292.909),85KY16PIN185161816PIN 180816150816PIN(-1.90294, 250.26, 294.934) ,Finger_type(tipposition)6877,277.055,299.705),棕櫚正常:(-0.739979,-0.472093,-0.479124),Finger_type(tipposition)TYPE_THUMB(-42.9838,294.545,305.915),Finger_type(tipposition)TYPE_INDEX(-26.5976,335.972,250.27),Finger_type (tipposition) TYPE_MIDDLE(-1.90294, 250.26, 294.934) ,Finger_type(tipposition) TYPE_RING(12.5955, 243.157, 292.909) ,Finger_type(tipposition) TYPE,85251(852.909) TYPE_PINKY(12.5955, 243.157, 292.909)6877,277.055,299.705),棕櫚正常:(-0.739979,-0.472093,-0.479124),Finger_type(tipposition)TYPE_THUMB(-42.9838,294.545,305.915),Finger_type(tipposition)TYPE_INDEX(-26.5976,335.972,250.27),Finger_type (tipposition) TYPE_MIDDLE(-1.90294, 250.26, 294.934) ,Finger_type(tipposition) TYPE_RING(12.5955, 243.157, 292.909) ,Finger_type(tipposition) 86,856181818131
文本檔案鏈接:https : //drive.google.com/file/d/1X1NdQNYlQWuNpmzGL6Wwi_SqFjIXnRbb/view? usp =sharing
uj5u.com熱心網友回復:
不是決議檔案的最復雜的方法,但似乎有效。
data = []
text = []
with open('data_file_1.txt') as f:
text = f.readlines()
text = ''.join(text) # Join all lines together, just in case
text = text.strip('\n').strip('\t') # Make whole text one line, just in case
rows = text.split('Frame.') # Separate rows of data
for row in rows[1:]: # First split is empty, start at second
row_data = {}
expected = 0
store = []
for item in row.split(','): # Split data at comma. Note: tuples will be split also
if expected: # If in last item was a start of tuple, add rest values here
expected -= 1
store[1].append(float(item.strip().strip(')')))
if not expected: # when all values are in place, save whole item to row_data
row_data[store[0]] = store[1]
store = []
# In single value cases, parse and save them
elif any(x in item for x in ['id', 'Timestamp', 'Hand_number', 'hand_Id_type', "hand_finger's_number"]):
name, value = item.split(':')
try:
value = int(value)
except ValueError:
value = value.strip()
row_data[name.strip()] = value
# In cases, where there are tuple of 3 floats, parse and store for save
elif any(x in item for x in ['Finger_type(tipposition)', 'Palm', 'direction']):
name, value = item.strip('Finger_type(tipposition) ').split('(')
store = [name.strip().strip(':'), [float(value)]]
expected = 2
data.append(row_data)
for row in data: # Final data in list of dict's
print(row)
# Export your data in format like: .csv, JSON, XML, what is suitable for use in your application
列印行,如
{'id': 263126, 'Timestamp': 697287019071, 'Hand_number': 1, 'hand_Id_type': '238right hand', "hand_finger's_number": 2, 'hand direction': [-0.142081, 0.865413, -0.480493], 'Palm position': [35.2841, 284.522, 330.828], 'Palm normal': [-0.686854, -0.435733, -0.581694], 'TYPE_THUMB': [-36.7239, 301.602, 330.845], 'TYPE_INDEX': [-14.9321, 347.039, 280.375], 'TYPE_MIDDLE': [5.5661, 258.191, 321.318], 'TYPE_RING': [20.0886, 251.219, 320.136], 'TYPE_PINKY': [27.5919, 259.584, 310.508]}
uj5u.com熱心網友回復:
我做了一些簡單的爭論并將其制作成 CSV。希望這對你有用:
lines = []
with open("data.txt") as f:
lines=f.readlines()
# Columns in file
header = "Frame.id,Timestamp,Hand_number,hand_Id_type,hand_finger's_number,hand direction,\
Palm position,Palm normal,Finger_type(tipposition) TYPE_THUMB,\
Finger_type(tipposition) TYPE_INDEX,Finger_type(tipposition) TYPE_MIDDLE,\
Finger_type(tipposition) TYPE_RING,Finger_type(tipposition) TYPE_PINKY\n"
out_lines = [header] # to store rows in output file
tmp=[] # empty list to store one row
for line in lines[1:]:
tmp.append(line.strip()) # strip and append
# if line has last column in row, add it to tmp, make the line
# add the line to list of rows and clear tmp
if line.strip().startswith(",Finger_type(tipposition) TYPE_PINKY"):
new_line = ''.join(tmp) '\n'
for column in header.split(','):
# replace header keywords and :
new_line = new_line.replace(column.strip(), '').replace(':','')
out_lines.append(new_line)
tmp=[]
print(''.join(out_lines))
# Uncomment the below block to write the csv file
# with open("output.csv","w ") as f:
# f.writelines(out_lines)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/318438.html
