小编mhl*_*ter的帖子

Python - 从csv文件中获取列数

我正在尝试确定python v2.6中CSV文件中存在的列数.这必须是一般的,因为对于我传递的任何输入,我应该能够获得文件中的列数.

示例输入文件: love hurt hit

其他输入文件: car speed beforeTune afterTune repair

到目前为止,我试图做的是读取文件(有很多行),获取第一行,然后计算第一行中的单词数.分界符是,.当我尝试headings根据示例输入进行拆分时遇到了问题,接下来len(headings)给了我14哪个错误,因为它应该给我3.任何想法?我是初学者.

with open(filename1, 'r') as f1:

 csvlines = csv.reader(f1, delimiter=',')

 for lineNum, line in enumerate(csvlines):
      if lineNum == 0:
           #colCount = getColCount(line)
           headings = ','.join(line)           # gives me  `love, hurt, hit`
           print len(headings)                 # gives me 14; I need 3
      else:
           a.append(line[0])
           b.append(line[1])
           c.append(line[2])
Run Code Online (Sandbox Code Playgroud)

python

3
推荐指数
1
解决办法
8542
查看次数

使用Python格式打印二维列表

所以我在python中定义了一个二维列表:

column = 3
row = 2
Matrix =  [['' for i in range(column)] for j in range(row)]
Run Code Online (Sandbox Code Playgroud)

然后我开始为它添加值:

Matrix[0][0] += 'A'
Matrix[1][0] += 'AB'
Matrix[2][0] += 'ABC'
Matrix[0][1] += 'X'
Matrix[1][1] += 'XY'
Matrix[2][1] += 'XYZ'
Run Code Online (Sandbox Code Playgroud)

然后我开始打印希望某种格式:

for i in range(0, row, 1):
    for j in range(0, column, 1):
        print(Matrix[i][j] + '\t')
Run Code Online (Sandbox Code Playgroud)

我在考虑获得结果

A   AB   ABC
X   XY   XYZ
Run Code Online (Sandbox Code Playgroud)

但实际上我得到了:

A   
AB  
ABC     
X   
XY  
XYZ 
Run Code Online (Sandbox Code Playgroud)

只是想知道我的代码有什么问题......

python

2
推荐指数
1
解决办法
341
查看次数

字符串中第二个重复字符的索引

我在python中尝试一个hangman代码.为了匹配单词的字符,iam使用索引函数来获取字符的位置.例如:word ='计算机'

user_input = raw_input('Enter a character :') # say 'T; is given here

if user_input in word:
                print "\nThe Character %c is present in the word \n" %user_input 
                word_dict[word.index(user_input)] = user_input

#so the output will looks like

{0: '_', 1: '_', 2: '_', 3: '_', 4: '_', 5: 'T', 6: '_', 7: '_'} 
Run Code Online (Sandbox Code Playgroud)

现在,我的问题来自于重复的角色.

# Another example 
>>> 'CARTOON'.index('O')
4
Run Code Online (Sandbox Code Playgroud)

对于第二个'O',如何获得其索引.因为我使用了这个'索引'逻辑,所以我希望继续这样做.

python string

1
推荐指数
1
解决办法
1万
查看次数

字符串格式化不支持的操作数

我正在尝试创建一个简单的程序,以mm/dd/yy形式打印日月和年,但我一直收到追溯错误:

%s/%s/%s
Traceback (most recent call last):
  File "C:/Python34/timenow.py", line 4, in <module>
    print('%s/%s/%s')%(now.month, now.day, now.year)
TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'
Run Code Online (Sandbox Code Playgroud)

我正在使用python 3.4.0

这是我的代码:

from datetime import datetime
now = datetime.now()

print('%s/%s/%s')%(now.month, now.day, now.year)
Run Code Online (Sandbox Code Playgroud)

python

1
推荐指数
1
解决办法
338
查看次数

为什么我会收到类型错误?

import time

def average(numbers):
    "Return the average (arithmetic mean) of a sequence of numbers."
    return sum(numbers) / float(len(numbers)) 

#example function

def domath(a,b,c):
    a+b+c
    a*b*c
    a/b/c
    a-b-c
    a^b
    b^c


def timedcalls(n, fn, *args):
    times=[]
    if type(n)==int:
        t0 = time.clock()
        for rep in range(n):
            t0 = time.clock()
            fn(*args)
            t1 = time.clock()
            times.append(t1-t0)
    else:
        start=time.clock()
        while time.clock-start<int(n):
            t0 = time.clock()
            fn(*args)
            t1 = time.clock()
            times.append(t1-t0)    
    return min(times), average(times), max(times)

print timedcalls(5.0, domath, 1,2,3)
Run Code Online (Sandbox Code Playgroud)

这个代码适用于int类型,但出于某种原因,如果我使用浮点数,它会给我这个错误.

Traceback (most recent call last):
  File "<stdin>", line 29, …
Run Code Online (Sandbox Code Playgroud)

python types

0
推荐指数
1
解决办法
538
查看次数

标签 统计

python ×5

string ×1

types ×1