所以我对python很新,我想做一些事情:
原始csv文件有3行
TTT TTTT, 21.72, 29.3, 20.08, 29.98, 29.85
DDDD, 57.51, 47.59
WWWW, 75.0, 82.43, 112.11, 89.93, 103.19, 80.6, 89.93, 103.19, 8
我已经在这几个小时了,我根本无法得到它我的最佳尝试是以下
with open('test1.csv', newline='') as f:
content = f.readlines()
print (content)
for line in content:
entry = line.split(',')
entry = line.split()
print (entry)
nums = 0
list1avg = 0
list1=[]
for c in entry:
if is_number(c):
print ("is num "+c)
list1.append(c)
nums+=1
else:
print(c)
for i in list1:
list1avg = list1avg +1
print(list1avg)
print(nums)
print("The average is "+list1avg/nums)
print("Total numbers in this row is " +nums)
我知道它一团糟,但感谢任何帮助。
答案 0 :(得分:0)
只是为了得到数字和行数,你可以做这样的事情
with open("csv_exmp.csv") as f:
items = [line.strip().split(',') for line in f]
for item in items:
numerbs_amount = sum(map(is_number, item))
print numerbs_amount, " in row:", item[0]
如果你想计算平均值等,只需将数据存储在for-loop
之外的一些变量中答案 1 :(得分:0)
正如Daniel Roseman所说,正确的方法是使用csv
库(不是外部的,而是python标准库的一部分)。当你看起来(无论出于何种原因)对不使用csv库感兴趣时,你应该注意到你可以根据子字符串而不仅仅是字符串来拆分字符串:
with open('test1.csv', newline='') as f:
for line in f.readlines():
line = line.strip() # remove the trailing `\n`
tokens = line.split(', ') # note comma and space
name = tokens[0]
numbers = list(map(float, tokens[1:]))
average = sum(numbers)/len(numbers)
print(numbers)
print("Name={}, number={}, average={}".format(name, len(numbers), average))
答案 2 :(得分:0)
这应该做你想要的,并且相当可读:
with open('test1.csv', newline='') as f:
for line in f:
cols = line.split(',')
numbers = [float(i) for i in cols[1:-1]]
mean = sum(numbers) / len(numbers)
name = cols[0]
print("The row contains %s" % ','.join(numbers))
print("The average for %s is %2f\n" % (name, mean))
如果有帮助,请告诉我!
编辑:
这是您想要做的其他事情。我省略了处理打印的代码,以及计算标准差,因为我早上太早才能记住数学。
with open('test1.csv', newline='') as f:
cols = {'count' : [], 'mean' : [], 'median' : [], 'minimum' : [], 'maximum' : [], 'std dev' : []}
names = []
for line in f:
row = line.split(',')
numbers = [float(i) for i in row[1:-1]]
num_count = len(numbers)
mean = sum(numbers) / num_count
name = row[0]
names.append(name)
cols['count'].append(num_count)
cols['mean'].append(mean)
cols['minimum'].append(min(numbers))
cols['maximum'].append(max(numbers))
要实际使用cols
,您可以执行以下操作:
for col in cols['count']:
print(col)