小编sk1*_*k11的帖子

在Python中从CSV文件读取数据

我正在从包含以下数据的CSV文件(xyz.CSV)中读取数据:

col1,col2,col3,col4
name1,empId1,241682-27638-USD-CIGGNT ,1
name2,empId2,241682-27638-USD-OCGGINT ,1
name3,empId3,241942-37190-USD-GGDIV ,2
name4,empId4,241942-37190-USD-CHYOF ,1
name5,empId5,241942-37190-USD-EQPL ,1
name6,empId6,241942-37190-USD-INT ,1
name7,empId7,242066-15343-USD-CYJOF ,3
name8,empId8,242066-15343-USD-CYJOF ,3
name9,empId9,242066-15343-USD-CYJOF ,3
name10,empId10,241942-37190-USD-GGDIV ,2
Run Code Online (Sandbox Code Playgroud)

当我用循环迭代它时,我能够通过以下代码按行打印数据并且只能打印column1数据.

file=open( path +"xyz.CSV", "r")
reader = csv.reader(file)
for line in reader:
    t=line[0]
    print t
Run Code Online (Sandbox Code Playgroud)

通过上面的代码我只能得到第一列.

如果我尝试打印行[1]或行[2],它会给我以下错误.

    file=open( path +"xyz.CSV", "r")
    reader = csv.reader(file)
    for line in reader:
        t=line[1],[2]
        print t

t=line[1],line[2]
IndexError: list index out of range
Run Code Online (Sandbox Code Playgroud)

请建议打印column2或column3的数据.

python csv

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

Aptana配置:配置的解释器在文件系统中不存在

几乎所有人都在标题中说.我已经更新到Aptana(在Windows上)的最新版本,现在我的解释器不起作用.我重新创建了PYTHONPATH变量,删除并重新配置了python解释器Aptana,删除并在我的项目中重新创建它,仍然没有工作...

它给了我以下错误:

The interpreter configured does not exist in the filesystem
Run Code Online (Sandbox Code Playgroud)

各种错误如:

Description Resource        Path        Location    Type
Undefined variable: None    models.py   line 48     PyDev Problem
Run Code Online (Sandbox Code Playgroud)

最糟糕的是,我在一个django应用程序上工作.我可以把它发射好,唯一的问题在于Aptana.任何线索?

python aptana django aptana3

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

用小写+额外字符替换大写字符

我试图在字符串中找到所有大写字母,并用小写加underscore字符替换它.AFAIK没有标准的字符串函数来实现这个(?)

例如,如果输入字符串是'OneWorldIsNotEnoughToLive'输出字符串应该是'_one_world_is_not_enough_to_live'

我能用以下代码完成:

# This finds all the uppercase occurrences and split into a list 
import re
split_caps = re.findall('[A-Z][^A-Z]*', name)
fmt_name = ''
for w in split_caps:
    fmt_name += '_' + w # combine the entries with underscore
fmt_name = fmt_name.lower() # Now change to lowercase
print (fmt_name)
Run Code Online (Sandbox Code Playgroud)

我觉得这太过分了.首先re,然后是列表迭代,最后转换为小写.也许有一种更简单的方法来实现这一点,更多的pythonic和1-2行.

请建议更好的解决方案 谢谢.

python regex string str-replace

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

标签 统计

python ×3

aptana ×1

aptana3 ×1

csv ×1

django ×1

regex ×1

str-replace ×1

string ×1