小编And*_*ndy的帖子

CsvReader下一个功能

我得到一个csv文件,但我想跳过第一行数据,然后继续下一步.这是我的代码:

def read_csv(inputfile):
    return list(csv.reader(inputfile)) #<-----

def generate_xml(reader,outfile):
    root = Element('Solution')
    root.set('version','1.0')
    tree = ElementTree(root)        
    head = SubElement(root, 'DrillHoles')
    description = SubElement(head,'description')
    current_group = None
    i = 1
    for row in reader.next(): #<-----
        x1,y1,z1,x2,y2,z2,cost = row
        if current_group is None or i != current_group.text:
            current_group = SubElement(description, 'hole',{'hole_id':"%s"%i})

            collar = SubElement(current_group,'collar')
            toe = SubElement(current_group,'toe')
            cost1 = SubElement(current_group,'cost')
            collar.text = ','.join((x1,y1,z1))
            toe.text = ','.join((x2,y2,z2))
            cost1.text = cost
        i+=1
    head.set('total_holes', '%s'%i)
    indent.indent(root)
    tree.write(outfile)
Run Code Online (Sandbox Code Playgroud)

如您所见,我将csv文件作为列表返回,然后将其传递给generate_xml函数.但是,当我运行完整的程序时,有一个

error: 'list' object has no attribute 'next'
Run Code Online (Sandbox Code Playgroud)

python csv

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

使用stringstream输入/输出bool值

这似乎是一个愚蠢的问题,但我很难过.这是我的代码:

int main()
{
    string line, command;
    getline(cin, line);
    stringstream lineStream(line);

    bool active;
    lineStream >>active;
    cout <<active<<endl;

}
Run Code Online (Sandbox Code Playgroud)

无论我输入什么活动,它总是打印出来0.所以我要说我的输入是

true
Run Code Online (Sandbox Code Playgroud)

它输出0和false同样的东西.

c++ stringstream

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

使用迭代器在python中对数组求和

我在python中使用Numpy来读取csv文件:

import numpy as np
import csv
from StringIO import StringIO
with open ('1250_12.csv','rb') as csvfile:
    data = np.genfromtxt(csvfile, dtype = None, delimiter = ',')
np.set_printoptions(threshold='nan'
Run Code Online (Sandbox Code Playgroud)

打印出以下内容:

[['x1' 'y1' 'z1' 'x2' 'y2' 'z2' 'cost']
 ['5720.44' '3070.94' '2642.19' '5797.82' '3061.01' '2576.29' '102.12']
 ['5720.44' '3070.94' '2642.19' '5809.75' '3023.6' '2597.81' '110.4']
 ['5861.54' '3029.08' '2742.36' '5981.23' '3021.52' '2720.47' '121.92']
 ['5861.54' '3029.08' '2742.36' '5955.36' '3012.95' '2686.28' '110.49']
Run Code Online (Sandbox Code Playgroud)

所以第一列属于'x1',第二列属于'x2'......等.假设x1,y1,z1是一个数组中表示的向量,下面的点代表该值.正如您所看到的,每个x1,y1等都有多个点.现在我想将这些点加起来,使它成为使用迭代器的向量之和.我如何使用迭代器来总结所有行?

像这样:

import numpy
a=numpy.array([0,1,2])
b=numpy.array([3,4,5])
a+b
array([3, 5, 7])
Run Code Online (Sandbox Code Playgroud)

但这只有2个数组,如果有数百个那么你需要一个迭代器而不是手动设置数组吗?

python csv arrays numpy

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

Python中的布尔运算符

为什么我不能这样做?

extout = os.path.splitext(args.outputfile)[1].lower()
if extout != (".csv" | ".xml"):  # <------- creates error
        sys.stderr.write('ERROR: Invalid extension or none found. This program only recognizes .csv or .xml extensions %s\n')
        sys.exit(1)
Run Code Online (Sandbox Code Playgroud)

这给了我一个类型错误:

Unsupported operand types for |: 'str' and 'str'
Run Code Online (Sandbox Code Playgroud)

python

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

Python中无限量的命令行参数

我正在尝试创建一个简单的计算器,它在命令行接受参数.例如,在命令行:

Calculator.py 1 2 44 6 -add
Run Code Online (Sandbox Code Playgroud)

会给我数字的总和.但是,用户如何输入无限量的参数.我知道你必须在函数中使用*args或类似的东西,我只是想知道如何使用argparse将它合并到命令行中.

python argparse

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

创建词法分析器

嘿伙计们,我正在尝试了解有关词法分析器的一些概念。我知道词法分析器在编译器中用于将字符串中的单个字符分成称为标记的形式。但让我困惑的是匹配部分。我不明白为什么我们需要将字符匹配到相应位置的逻辑。

import sys
import re

def lex(characters, token_exprs):
    pos = 0
    tokens = []
    while pos < len(characters):
        match = None
        for token_expr in token_exprs:
            pattern, tag = token_expr
            regex = re.compile(pattern)
            match = regex.match(characters, pos)
            if match:
                text = match.group(0)
                if tag:
                    token = (text, tag)
                    tokens.append(token)
                break
        if not match:
            sys.stderr.write('Illegal character: %s\n' % characters[pos])
            sys.exit(1)
        else:
            pos = match.end(0)
    return tokens
Run Code Online (Sandbox Code Playgroud)

这是我不完全理解的代码。在 for 循环之后,我不太明白代码要做什么。为什么我们必须将字符与位置匹配?

python lexer

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

标签 统计

python ×5

csv ×2

argparse ×1

arrays ×1

c++ ×1

lexer ×1

numpy ×1

stringstream ×1