如何从python脚本读取csv文件?
大家好, 我想通过我的Python脚本阅读CSV文件的内容. CSV文件说n行,每行包含一些逗号分隔的数字. 我必须单独访问特定行,然后分别访问所有数字. 对于ex:CSV文件就是这样: 1 01,02,05,07,18 2 00.01,04 3 09,20,21 现在,第一个数字是该行的数字,将作为输入thru命令行args提供. 我想获得以下行号之后的数字...即,对于第1行,我想获得01,02,05,07,18不是一条完整的行,而是单独的数字. 请帮助
- >>> for line in open("file"):
- ... line = line.strip().split()
- ... print "row: ", line[0] , line[1:][0].split(",")
- ...
- row: 1 ['01', '02', '05', '07', '18']
- row: 2 ['00.01', '04']
- row: 3 ['09', '20', '21']
- '''
- 1 01,02,05,07,18
- 2 00,01,04
- 3 09,20,21
- 4 12,34,56,77,88,99
- 5 03,05,77,88,54
- '''
- def file_data1(f):
- dd = {}
- for line in f:
- dd[int(line.split()[0])] = [int(i) for i in line.split()[1].split(',')]
- return dd
- dd = file_data1(open('your_file').readlines())
- for key in dd:
- print '%s = %s' % (key, dd[key])
- '''
- >>> 1 = [1, 2, 5, 7, 18]
- 2 = [0, 1, 4]
- 3 = [9, 20, 21]
- 4 = [12, 34, 56, 77, 88, 99]
- 5 = [3, 5, 77, 88, 54]
- '''
>>>因为我在DD [5]中: ...打印我, ... 3 5 77 88 54 >>>
- import sys
- choice = sys.argv[1]
- for line in open("file"):
- line = line.strip().split()
- if choice == line[0]
- print line[1:][0].split(",")
- def file_data1(f):
- dd = {}
- for line in f:
- dd[int(line.split()[0])] = [int(i) for i in line.split()[1].split(',')]
- return dd
您在上述功能中看到逗号吗? 您的数据具有半色调,因此您需要将其更改为:
- def file_data1(f):
- dd = {}
- for line in f:
- dd[int(line.split()[0])] = [int(i) for i in line.split()[1].split(';')] # chaned here
- return dd
感谢您,BV,提供原始功能.
- Traceback (most recent call last):
- File "<stdin>", line 1, in ?
- File "<stdin>", line 4, in file_dat
- IndexError: list index out of range
- import sys
- choice = sys.argv[1]
- for line in open("file"):
- line = line.strip().split()
- if choice == line[0]:
- print line[1:][0].split(";")
Outpu t:
- # ./test.py 23
- ['11', '12', '']
请注意,您的CSV文件现在已结肠分离,而不是分离逗号. 因此,您必须更改split()语句.
- Traceback (most recent call last):
- File "<stdin>", line 1, in ?
- File "<stdin>", line 4, in file_data
- ValueError: invalid literal for int(): Of
[/引用]
- Traceback (most recent call last):
- File "<stdin>", line 1, in ?
- File "<stdin>", line 4, in file_data
- ValueError: invalid literal for int(): Of
- import os
- import sys
- import csv
- def main(argv):
- choice = sys.argv[1]
- fp = open("c:\pst\pst_modified\log_4_0.csv",'r')
- for line in fp:
- line = line.strip().split()
- if choice == line[0]:
- print line[1:][0].split(";")
3)我在系统上安装了Python23 4)打开命令提示 5)移至放置新的目录的目录 6)执行以下命令 python new.py'23' 我错了吗? . 不是有效的命令.
- import os
- import sys
- import csv ## you are not using this?
- filepath = os.path.join("C:\\","pst","pst_modified","log_4_0.csv")
- def main():
- choice = sys.argv[1]
- for line in open(filepath):
- line = line.strip().split()
- if choice == line[0]:
- print line[1:][0].split(";")
- main() ## < --you did not call the main in your last posted script sample.
- def main():
- choice = sys.argv[1]
- for line in open(filepath):
- line = line.strip().split()
- print line, type(line), line[0], line[1]
- raw_input("Press to continue: " )
- if choice == line[0]:
- print line[1:][0].split(";")
- main()
请显示上述输出.
- [',List', 'Of', 'Individual', 'Patches'] <type 'list'> ,List Of
- Press to continue:
- ['1,00;01;'] <type 'list'> 1,00;01;
- Traceback (most recent call last):
- File "<stdin>", line 1, in ?
- File "my.py", line 12, in main
- print line, type(line), line[0], line[1]
- IndexError: list index out of range
- List Of Individual Patches
- 1 00;01;
- 2 00;
- 3 01;
- 4 02;
- 5 00;01;02;
- 6 01;02;
- 7 01;02;03;04;05;
- 8 01;02;03;04;05;06;
- 9 00;01;02;03;05;
- 10 01;02;03;05;
- 11 02;03;05;06;
- 12 04;
- 13 01;02;03;05;08;09;
- 14 00;01;02;03;05;09;
- 15 00;01;02;03;05;09;10;
- 16 01;02;03;05;08;09;10;
- 17 01;02;03;05;08;10;
- 18 08;
- 19 00;01;02;03;05;08;09;10;
- 20 02;03;05;06;08;10;
- 21 11;
- 22 12;
- 23 11;12;
- 24 00;01;02;03;05;14;
因此,如果您已将格式更改为喜欢:
- 1,00;01;
- 2,00;
- ...
- ..
然后肯定是行不通的. 您必须修改代码. 我给您提示在哪里更改:
- ....
- for line in open(filepath):
- line = line.strip().split() <---here.
- ....
- [',List', 'Of', 'Individual', 'Patches'] <type 'list'> ,List Of
- Press to continue:
- ['1,00;01;'] <type 'list'> 1,00;01;
- Traceback (most recent call last):
- File "<stdin>", line 1, in ?
- File "my.py", line 12, in main
- print line, type(line), line[0], line[1]
- IndexError: list index out of range
Follwing输出线从"打印行,类型(线),行[0],行[1]"陈述:
- ...
- ['1,00;01;'] <type 'list'> 1,00;01;
- ...
拆分后的"线"值是['1,00; 01;']. 这意味着它根本没有分裂. 此外,line.strip().split()在空白上拆分,并且由于您的数据现在有一个逗号,因此无能为力. 注意第一个" 1"之后的""? 这就是为什么我推论您的CSV格式不同. 如果否则,我一点都不知道.
- ...
- line = line.strip().split(",")
- ...
- line = line.strip().split(",")
代替这个
- line = line.strip().split()
- 1,00;01;
- 2,00;
- ....
- ...
如果不是这样,那么逗号上的分裂将不起作用.
标签: python