1
我是編碼和學習python的新手。我一直在試圖創建一個程序,詢問用戶有關某個零件的信息,然後將其附加到一個文件中。一旦它附加了文件,我應該能夠看到文件中的信息,如果我打開它。但是,該程序不會將信息保存在文件上。我可能會犯一個愚蠢的錯誤,但我一直無法弄清楚,因爲我對編碼完全陌生。無法在數據幀行中附加csv文件
import sys
import pandas as pd
colnames = ['name', 'numid', 'length', 'height']
parts_info = pd.read_csv('part.info', sep ='\t', header = None, names = colnames, index_col = 'name')
New_parts = {}
class Part:
name = ""
numid = 0
height = 0
length = 0
def display(self):
print ''
print 'Part Information:'
print parts_info
def get(self):
self.name = raw_input('Enter Part Name: ')
self.numid = int(raw_input('Enter NumId: '))
self.height = float(raw_input('Enter Height (in feet): '))
self.length = int(raw_input('Enter Length: '))
def new_part(self):
New_parts[self.name] = {'numid':self.numid, 'height':self.height, 'length':self.length}
def save(self):
with open('part.info','a') as f:
parts_info.to_csv(f, header = False)
f.close()
onePart = None
if len(sys.argv) > 1 and sys.argv[1] == 'READ':
onePart = Part()
else:
onePart = Part()
onePart.get()
onePart.new_part()
onePart.save()
New_df = pd.DataFrame.from_dict(New_parts,orient='index')
New_df.index.name = 'name'
parts_info = parts_info.append(New_df)
onePart.display()
歡迎來到堆棧溢出! – tmthydvnprt