[英]Python CSV reading - I think I'm making a list of lists but I just want a list
This regards Python 2.7 关于Python 2.7
I have a csv file, soq1, that looks like this in Notepad: 我有一个csv文件soq1,在记事本中看起来像这样:
I used this code to read in the file and display the contents of the list soq1: 我使用以下代码读取文件并显示列表soq1的内容:
import csv
with open('D:\Users\Richard\Python\soq1.csv','rb') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
soq1=list(readCSV)
print(soq1)
print(type(soq1))
print(len(soq1))
I was expecting soq1 to look like: ['2','3','4','5','6'] 我期望soq1看起来像:['2','3','4','5','6']
In other words, I did not expect to have the extra set of square brackets. 换句话说,我没想到会有额外的方括号。 Did I create a list of lists?
我是否创建了列表列表?
What did I do wrong? 我做错了什么?
Yes, you created a list of lists, but this is intentional because by nature, CSV files are meant to have separate entries delimited by new lines, and separate properties in each entry delimited by commas (why they call it comma separated values). 是的,您创建了一个列表列表,但这是有意的,因为从本质上来说,CSV文件意味着具有以换行分隔的单独条目,并以逗号分隔每个条目中的单独属性(为什么将其称为逗号分隔值)。
That is not a proper CSV file, by the way. 顺便说一下,那不是正确的CSV文件。 The convention is that the first line (also known as the header line) should denote comma-separated strings describing what each value means in successive lines/entries.
按照惯例,第一行(也称为标题行)应表示逗号分隔的字符串,这些字符串描述每个值在连续的行/条目中的含义。
If you would like to read that file and produce ['2','3','4','5','6']
, csv.reader
is not suited to your specific use case. 如果您想读取该文件并生成
['2','3','4','5','6']
,则csv.reader
不适合您的特定用例。 You will want to read each separate line and append it to a list, or read the entire file in and split it into a list using \\n
as a delimiter. 您将需要读取每一行并将其附加到列表中,或者使用
\\n
作为分隔符读取其中的整个文件并将其拆分为列表。
Each of the numbers is on a separate line, this is causing the csv reader to think they are separate rows rather than columns. 每个数字都在单独的行上,这使csv阅读器认为它们是单独的行而不是列。 You should change the csv file if you can as that is not the correct format of a csv file for one row.
如果可以,则应更改csv文件,因为那不是一行的csv文件的正确格式。
You didn't do anything wrong - the csv
module just returns one list per line (which makes sense for CSVs with multiple entries in each line.) 您没有做错任何事情
csv
模块仅每行返回一个列表(这对于每行中包含多个条目的CSV有意义)。
You can flatten your list using 您可以使用以下方式拼合列表
soq2 = [elt for lst in soq1 for elt in lst]
Although for such a simple file, you don't really need to handle it as a CSV at all (it doesn't matter what the file extension is.) You could just do: 尽管对于这样一个简单的文件,您实际上根本不需要将其作为CSV处理(文件扩展名是什么都没有关系。)您可以这样做:
with open(my_file) as f:
soq1 = [line.strip() for line in f]
The csv.reader
creates a list for each line in the file. csv.reader
为文件中的每一行创建一个列表。 Which makes sense in most cases, because most csv files aren't a single column. 在大多数情况下,这很有意义,因为大多数csv文件不是单个列。
You can always flatten the list with a comprehension: 您始终可以通过以下方式弄平列表:
foo = [item for inner in outer for item in inner]
Improved the code 改进了代码
import csv
with open('D:\Users\Richard\Python\soq1.csv','rb') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
soq1=list(readCSV) #commnt this line
for line in readCSV:
print line
print type(line)
print len(line)
#print(soq1)
#print(type(soq1))
#print(len(soq1))
Decided to move my comment to your question to an answer, because I feel like all the answers here do not reflect what you want to achieve. 决定将我对您的问题的评论移至答案,因为我觉得这里的所有答案都不能反映您想要实现的目标。
You get a list of lists because each line in a CSV generally has multiple columns. 您会得到一个列表列表,因为CSV中的每一行通常都有多列。
If that's not the case, why do all the CSV stuff? 如果不是这种情况,为什么要使用所有CSV文件? You can just read your text file into a flat list.
您可以将文本文件读入平面列表中。
How do I read a file line-by-line into a list? 如何将文件逐行读入列表?
lines = tuple(open(filename, 'r'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.