我对python和编码很陌生。我正在尝试编写一个代码,从电线列表中打印出每个给定电线类型的总量。这是工作的附带项目。我能够想出一个代码来总结用户定义的导线类型的所有导线。现在,我想制作另一个代码,打印出文件中每个导线类型的总数。
这是我为总结用户选择的单个导线类型而编写的代码。
wtype = []
w = []
w1 = []
#opens the .TXT file
fhand = input('nEnter Text Filen')
try:
if (len(fhand) <= 0):
fhand = 'test.txt'
fh = open(fhand)
except:
print('nNo File Found:', fhand, 'n')
exit()
#prints out the possible wire types
for line in fh:
line = line.rstrip()
wtype.append(line) #needed for later in the code
line2 = line.split(',')[2]
if line2 not in w:
w.append(line2)
else:
continue
d1 = dict(enumerate(w))
print(d1)
#sums up the selected wire types total length from the given .TXT file
wire = int(input('nEnter the number that is before the wire type you need:n'))
for key, val in d1.items():
if key == wire:
for x in wtype:
x = x.split(',')
if x[2] == val:
w1.append(x[1])
else:
continue
s = [eval(i) for i in w1]
print('nYour will need ', sum(s)/12, ' Feet of ', val, '.n')
这是test.txt
文件,长度以英寸为单位,在代码sum(s)/12
:的最后一行转换为英尺
该文件的列为WIRE、LENGTH、TYPE和QTY。
WIRE-006A22,72,M22759/16-22-9,1
WIRE-005A22,60,M22759/16-22-9,1
WIRE-004A22,72,M22759/16-22-9,1
WIRE-003A22,72,M22759/16-20-9,1
WIRE-002A22,60,M22759/16-20-9,1
WIRE-001A22,72,M22759/16-22-9,1
WIRE-009A22,72,M22759/16-22-9,1
WIRE-008A22,60,M22759/16-22-9,1
WIRE-007A22,72,M22759/16-20-9,1
WIRE-011A22,72,M22759/16-22-9,1
WIRE-012A22,72,M22759/16-22-9,1
WIRE-014A22,72,M22759/16-20-9,1
WIRE-013A22,60,M22759/16-22-9,1
WIRE-021A22,72,M22759/16-20-9,1
WIRE-031A22,72,M22759/16-22-9,1
WIRE-032A22,72,M22759/16-20-9,1
WIRE-043A22,60,M22759/16-22-9,1
WIRE-054A22,72,M22759/16-20-9,1
WIRE-065A22,72,M22759/16-22-9,1
WIRE-076A22,60,M22759/16-22-9,1
WIRE-087A22,72,M22759/16-22-9,1
WIRE-098A22,72,M22759/16-20-9,1
WIRE-089A22,72,M22759/16-20-9,1
WIRE-078A22,72,M22759/16-20-9,1
WIRE-067A22,60,M22759/16-22-9,1
WIRE-056A22,72,M22759/16-22-9,1
WIRE-045A22,72,M22759/16-20-9,1
WIRE-034A22,60,M22759/16-22-9,1
WIRE-023A22,60,M22759/16-22-9,1
WIRE-012A22,72,M22759/16-20-9,1
我希望尝试并实现的输出是:
output: {'M22759/16-22-9': 100, 'M22759/16-20-9': 71}
并可扩展到d1
中的所有不同导线类型
这里有一种使用pandas库的简单方法,只需很少的代码。
import pandas
df = pandas.read_csv("test.csv")
df_out = df.groupby("TYPE")["QTY"].sum()
print("Output:", df_out.to_dict())
# Output: {'M22759/16-20-9': 12, 'M22759/16-22-9': 18}
它假设输入的CSV文件如下所示:
WIRE,LENGTH,TYPE,QTY
WIRE-006A22,72,M22759/16-22-9,1
WIRE-005A22,60,M22759/16-22-9,1
WIRE-004A22,72,M22759/16-22-9,1
WIRE-003A22,72,M22759/16-20-9,1
...
如果CSV文件没有标题,那么您仍然可以使用panda。只需告诉它没有标题,然后使用列编号而不是列名。例如:
import pandas
df = pandas.read_csv("test-noheader.csv", header=None)
df_out = df.groupby(2)[3].sum()
print("Output:", df_out.to_dict())
当然,只需使用非panda代码就可以获得同样的结果,但我认为值得分享的是,这可能只有几行代码
下面是一个使用标准csv模块的简单非panda版本:
import csv
output = {}
with open("test.csv") as csvfile:
for row in csv.DictReader(csvfile):
if row["TYPE"] in output:
output[row["TYPE"]] += int(row["QTY"])
else:
output[row["TYPE"]] = int(row["QTY"])
print("Output:", output)
同样,如果CSV文件没有标题:
import csv
output = {}
with open("test-noheader.csv") as csvfile:
for row in csv.DictReader(csvfile, fieldnames=["WIRE", "LENGTH", "TYPE", "QTY"]):
if row["TYPE"] in output:
output[row["TYPE"]] += int(row["QTY"])
else:
output[row["TYPE"]] = int(row["QTY"])
print("Output:", output)
PS您的文本文件实际上是一个csv文件,因此最好相应地命名它(例如test.csv
(。
与您所做的类似,但在迭代时生成字典后会运行整个文件。
import collections
d = collections.defaultdict(int)
with open('thefile.txt') as f:
for line in f:
wire,length,type,qty = line.strip().split(',')
d[type] += int(length)
for type,l in d.items():
print(type,l)
>>>
M22759/16-22-9 1200
M22759/16-20-9 852
对于英尺而非英寸:
import collections
d = collections.defaultdict(float)
with open('thefile.txt') as f:
next(f)
for line in f:
wire,length,type,qty = line.strip().split(',')
d[type] += int(length)/12
我建议使用csv文件,以便对数据进行正确排序。我有一些东西希望能让你开始。它不包括你的输入选项,但希望这对你有用。我有一些Python经验,但按照我的标准,我仍然被认为是初学者
import pandas as py
data = py.read_csv('testfile.csv').sort_values(by=['col3'])
wtype = []
w = []
w1 = []
data_dict = {}
this_val = ''
for x in data.iterrows():
wire_name = x[1][2]
if this_val == wire_name:
data_dict[wire_name] += x[1][1]
else:
data_dict[wire_name] = x[1][1]
this_val = wire_name
#Removed to use dict comp
data_dict = {key:int(val/12) for key,val in data_dict.items()}
#for key,val in data_dict.items():
#data_dict[key] = int(val/12)
print(data_dict)