如何从python脚本中读取csv文件? [英] How to read a csv file from a python script?

查看:85
本文介绍了如何从python脚本中读取csv文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想通过我的python脚本读取csv文件的内容。

csv文件说n行,每行包含一些数字被逗号隔开。我必须分别访问特定行和所有数字。例如:csv文件是这样的:


1 01,02,05,07,18

2 00.01,04

3 09,20,21


现在第一个数字是行号将通过命令行args作为输入提供。

i想得到行号后面的数字...即第1行我想获得01,02,05,07,18不是作为一个完整的行而是作为单独的数字。

plz help

Hi all,
I want to read the contents of a csv file thru my python script.
The csv file has say n rows and each row contains some numbers separated by commas. I have to access a specific row and then all the numbers separately. For ex: the csv file is something like this:

1 01,02,05,07,18
2 00.01,04
3 09,20,21

now the first number which is the number of the row will be provided as input thru command line args.
i want to get the numbers following the row number...i.e for row 1 i want to get 01,02,05,07,18 not as one complete line but as separate numbers.
plz help

推荐答案


大家好,

我想通过csv文件读取内容我的python脚本。

csv文件说n行,每行包含一些用逗号分隔的数字。我必须分别访问特定行和所有数字。例如:csv文件是这样的:


1 01,02,05,07,18

2 00.01,04

3 09,20,21


现在第一个数字是行号将通过命令行args作为输入提供。

i想得到行号后面的数字...即第1行我想获得01,02,05,07,18不是作为一个完整的行而是作为单独的数字。

plz help
Hi all,
I want to read the contents of a csv file thru my python script.
The csv file has say n rows and each row contains some numbers separated by commas. I have to access a specific row and then all the numbers separately. For ex: the csv file is something like this:

1 01,02,05,07,18
2 00.01,04
3 09,20,21

now the first number which is the number of the row will be provided as input thru command line args.
i want to get the numbers following the row number...i.e for row 1 i want to get 01,02,05,07,18 not as one complete line but as separate numbers.
plz help



,因为你的数据在行号之后有一个很大的空格,而在其他地方,你可以在空白处使用split()。

例如


since your data has a significant blank space just after the row number and nowhere else, you can use split() on blank.
eg

展开 | 选择 | Wrap | 行号



大家好,

我想通过我的python脚本读取csv文件的内容。

csv文件说n行,每行包含一些用逗号分隔的数字。我必须分别访问特定行和所有数字。例如:csv文件是这样的:


1 01,02,05,07,18

2 00.01,04

3 09,20,21


现在第一个数字是行号将通过命令行args作为输入提供。

i想得到行号后面的数字...即第1行我想获得01,02,05,07,18不是作为一个完整的行而是作为单独的数字。

plz help
Hi all,
I want to read the contents of a csv file thru my python script.
The csv file has say n rows and each row contains some numbers separated by commas. I have to access a specific row and then all the numbers separately. For ex: the csv file is something like this:

1 01,02,05,07,18
2 00.01,04
3 09,20,21

now the first number which is the number of the row will be provided as input thru command line args.
i want to get the numbers following the row number...i.e for row 1 i want to get 01,02,05,07,18 not as one complete line but as separate numbers.
plz help



我的朋友ghostdog74有一个非常好的解决方案,如果事实上行以行号开头且条目之间没有空格。

A真正的csv文件的格式更像是:

1st_value,2nd_value等

对于这样的文件,csv模块也是一个不错的选择。

My friend ghostdog74 has a very good solution if, in fact, rows start with row number and there are NO spaces between entries.
A true csv file would have a format more like:
1st_value, 2nd_value, etc
For such files, the csv module may be a good choice, too.



我的朋友ghostdog74有一个非常好的解决方案,实际上,行以行号开头且条目之间没有空格。

一个真正的csv文件的格式更像是:
1st_value,2nd_value等

对于这样的文件,csv模块也是一个不错的选择。
My friend ghostdog74 has a very good solution if, in fact, rows start with row number and there are NO spaces between entries.
A true csv file would have a format more like:
1st_value, 2nd_value, etc
For such files, the csv module may be a good choice, too.



实际上情况有点不同..

csv文件中有几行,每行都以一个唯一的数字开头用另一组数字。正如我在上一篇文章中所说的那样。我必须访问一个特定的行和它后面的数字。


Actually the situation is little bit different..
there are several rows in the csv file and each row starts with a unique number followed by another set of numbers. as i told in the previous post. I have to access a particular row and the numbers following it.


这篇关于如何从python脚本中读取csv文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆