Python 如何从 CSV 文件导入数据并将其存储在变量中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23903094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to import data from a CSV file and store it in a variable?
提问by user3682157
I am extremely new to python 3 and I am learning as I go here. I figured someone could help me with a basic question: how to store text from a CSV file as a variable to be used later in the code. So the idea here would be to import a CSV file into the python interpreter:
我对 python 3 非常陌生,我在这里学习。我想有人可以帮助我解决一个基本问题:如何将 CSV 文件中的文本存储为稍后在代码中使用的变量。所以这里的想法是将一个 CSV 文件导入到 python 解释器中:
import csv
with open('some.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
...
and then extract the text from that file and store it as a variable (i.e. w = ["csv file text"]
) to then be used later in the code to create permutations:
然后从该文件中提取文本并将其存储为变量(即w = ["csv file text"]
),以便稍后在代码中使用以创建排列:
print (list(itertools.permutations(["w"], 2)))
If someone could please help and explain the process, it would be very much appreciated as I am really trying to learn. Please let me know if any more explanation is needed!
如果有人可以帮助并解释这个过程,我将非常感激,因为我真的很想学习。如果需要更多解释,请告诉我!
采纳答案by mhawke
itertools.permutations()
wants an iterable (e.g. a list) and a length as its arguments, so your data structure needs to reflect that, but you also need to define what you are trying to achieve here. For example, if you wanted to read a CSV file and produce permutations on every individual CSV field you could try this:
itertools.permutations()
想要一个可迭代的(例如一个列表)和一个长度作为它的参数,所以你的数据结构需要反映这一点,但你还需要定义你想要在这里实现的目标。例如,如果您想读取 CSV 文件并在每个单独的 CSV 字段上生成排列,您可以尝试以下操作:
import csv
with open('some.csv', newline='') as f:
reader = csv.reader(f)
w = []
for row in reader:
w.extend(row)
print(list(itertools.permutations(w, 2)))
The key thing here is to create a flat list that can be passed to itertools.permutations()
- this is done by intialising w
to an empty list, and then extending its elements with the elements/fields from each row of the CSV file.
这里的关键是创建一个可以传递给的平面列表itertools.permutations()
——这是通过初始化w
为一个空列表,然后使用 CSV 文件每一行中的元素/字段扩展其元素来完成的。
Note: As pointed out by @martineau, for the reasons explained here, the file should be opened with newline=''
when used with the Python 3 csv module.
注意:正如@martineau 所指出的,出于此处解释的原因,newline=''
当与 Python 3 csv 模块一起使用时,应打开该文件。
回答by Burhan Khalid
Is this what you need?
这是你需要的吗?
import csv
with open('some.csv', 'rb') as f:
reader = csv.reader(f, delimiter=',')
rows = list(reader)
print('The csv file had {} rows'.format(len(rows)))
for row in rows:
do_stuff(row)
do_stuff_to_all_rows(rows)
The interesting line is rows = list(reader)
, which converts each row from the csv file (which will be a list), into another list rows
, in effect giving you a list of lists.
有趣的一行是rows = list(reader)
,它将 csv 文件(将是一个列表)中的每一行转换为另一个 list rows
,实际上为您提供了一个列表列表。
If you had a csv file with three rows, rows
would be a list with three elements, each element a row representing each line in the original csv file.
如果您有一个包含三行的 csv 文件,rows
则将是一个包含三个元素的列表,每个元素一行代表原始 csv 文件中的每一行。
回答by Rohit
If all you care about is to read the raw text in the file (csv
or not) then:
如果您只关心读取文件中的原始文本(csv
或不读取),那么:
with open('some.csv') as f:
w = f.read()
will be a simple solution to having w="csv, file, text\nwithout, caring, about columns\n"
将是一个简单的解决方案 w="csv, file, text\nwithout, caring, about columns\n"
回答by shengy
First, a csv
file is a text file too, so everything you can do with a file, you can do it with a csv
file. That means f.read()
, f.readline()
, f.readlines()
can all be used. see detailed information of these functions here.
首先,csv
文件也是文本文件,所以你可以用文件做的任何事情,你都可以用文件来做csv
。这意味着f.read()
, f.readline()
,f.readlines()
都可以使用。在此处查看这些功能的详细信息。
But, as your file is a csv
file, you can utilize the csv
module.
但是,由于您的文件是一个csv
文件,您可以使用该csv
模块。
# input.csv
# 1,david,enterprise
# 2,jeff,personal
import csv
with open('input.csv') as f:
reader = csv.reader(f)
for serial, name, version in reader:
# The csv module already extracts the information for you
print serial, name, version
More details about the csv
module is here.
有关该csv
模块的更多详细信息,请参见此处。
回答by jrjc
回答by pepr
If you want to use Python 3 (as you state in the question) and to process the CSV file using the standard csv
module, you should be careful about how to open the file. So far, your code and the answers use the Python 2 way of opening the CSV file. The things has changed in Python 3.
如果您想使用 Python 3(如您在问题中所述)并使用标准csv
模块处理 CSV 文件,您应该注意如何打开文件。到目前为止,您的代码和答案使用 Python 2 方式打开 CSV 文件。Python 3 中的事情发生了变化。
As shengywrote, the CSV file is just a text file, and the csv
module gets the elements as strings. Strings in Python 3 are unicode strings. Because of that, you should open the file in the text mode, and you should supply the encoding. Because of the nature of CSV file processing, you should also use the newline=''
when opening the file.
正如shengy所写,CSV 文件只是一个文本文件,csv
模块以字符串的形式获取元素。Python 3 中的字符串是 unicode 字符串。因此,您应该以文本模式打开文件,并提供编码。由于 CSV 文件处理的性质,您还应该newline=''
在打开文件时使用。
Now extending the explanation of Burhan Khalid... When reading the CSV file, you get the rows as lists of strings. If you want to read all content of the CSV file into memory and store it in a variable, you probably want to use the list of rows (i.e. list of lists where the nested lists are the rows). The for
loop iterates through the rows. The same way the list()
function iterates through the sequence (here through the sequence of rows) and build the list of the items. To combine that with the wish to store everything in the content
variable, you can write:
现在扩展对Burhan Khalid的解释......读取 CSV 文件时,您将获得作为字符串列表的行。如果要将 CSV 文件的所有内容读入内存并将其存储在变量中,您可能需要使用行列表(即嵌套列表为行的列表列表)。该for
循环迭代通过行。list()
函数以相同的方式遍历序列(这里是通过行序列)并构建项目列表。要将其与将所有内容存储在content
变量中的愿望结合起来,您可以编写:
import csv
with open('some.csv', newline='', encoding='utf_8') as f:
reader = csv.reader(f)
content = list(reader)
Now you can do your permutation as you wish. The itertools
is the correct way to do the permutations.
现在您可以根据需要进行排列。这itertools
是进行排列的正确方法。
回答by MONTYHS
import csv
data = csv.DictReader(open('FileName.csv', 'r'))
print data.fieldnames
output = []
for each_row in data:
row = {}
try:
p = dict((k.strip(), v) for k, v in p.iteritems() if v.lower() != 'null')
except AttributeError, e:
print e
print p
raise Exception()
//based on the number of column
if p.get('col1'):
row['col1'] = p['col1']
if p.get('col2'):
row['col2'] = p['col2']
output.append(row)
Finally all data stored in output variable
最后所有数据存储在输出变量中