Python csv 字符串到数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3305926/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:23:01  来源:igfitidea点击:

Python csv string to array

pythonstringarrayscsv

提问by Drew LeSueur

Anyone know of a simple library or function to parse a csv encoded string and turn it into an array or dictionary?

任何人都知道一个简单的库或函数来解析 csv 编码的字符串并将其转换为数组或字典?

I don't think I want the built in csv modulebecause in all the examples I've seen that takes filepaths, not strings.

我不认为我想要内置的csv 模块,因为在我看到的所有示例中,都使用文件路径,而不是字符串。

采纳答案by Micha? Niklas

You can convert a string to a file object using io.StringIOand then pass that to the csvmodule:

您可以使用将字符串转换为文件对象io.StringIO,然后将其传递给csv模块:

from io import StringIO
import csv

scsv = """text,with,Polish,non-Latin,letters
1,2,3,4,5,6
a,b,c,d,e,f
g??,zó?ty,w??,idzie,w?sk?,dró?k?,
"""

f = StringIO(scsv)
reader = csv.reader(f, delimiter=',')
for row in reader:
    print('\t'.join(row))

simpler version with split()on newlines:

带有split()换行符的更简单版本:

reader = csv.reader(scsv.split('\n'), delimiter=',')
for row in reader:
    print('\t'.join(row))

Or you can simply split()this string into lines using \nas separator, and then split()each line into values, but this way you must be aware of quoting, so using csvmodule is preferred.

或者您可以简单地split()将此字符串\n用作分隔符,然后将split()每一行转换为值,但这种方式必须注意引用,因此csv首选使用模块。

On Python 2you have to import StringIOas

Python 2 上,您必须导入StringIO

from StringIO import StringIO

instead.

反而。

回答by adamk

Simple - the csv module works with lists, too:

简单 - csv 模块也适用于列表:

>>> a=["1,2,3","4,5,6"]  # or a = "1,2,3\n4,5,6".split('\n')
>>> import csv
>>> x = csv.reader(a)
>>> list(x)
[['1', '2', '3'], ['4', '5', '6']]

回答by roskakori

As others have already pointed out, Python includes a module to read and write CSV files. It works pretty well as long as the input characters stay within ASCII limits. In case you want to process other encodings, more work is needed.

正如其他人已经指出的那样,Python 包含一个用于读取和写入 CSV 文件的模块。只要输入字符保持在 ASCII 限制内,它就可以很好地工作。如果您想处理其他编码,则需要做更多工作。

The Python documentation for the csv moduleimplements an extension of csv.reader, which uses the same interface but can handle other encodings and returns unicode strings. Just copy and paste the code from the documentation. After that, you can process a CSV file like this:

csv 模块Python 文档实现了 csv.reader 的扩展,它使用相同的接口但可以处理其他编码并返回 unicode 字符串。只需复制并粘贴文档中的代码即可。之后,您可以像这样处理 CSV 文件:

with open("some.csv", "rb") as csvFile: 
    for row in UnicodeReader(csvFile, encoding="iso-8859-15"):
        print row

回答by nvd

>>> a = "1,2"
>>> a
'1,2'
>>> b = a.split(",")
>>> b
['1', '2']

To parse a CSV file:

解析 CSV 文件:

f = open(file.csv, "r")
lines = f.read().split("\n") # "\r\n" if needed

for line in lines:
    if line != "": # add other needed checks to skip titles
        cols = line.split(",")
        print cols

回答by ivan_pozdeev

https://docs.python.org/2/library/csv.html?highlight=csv#csv.reader

https://docs.python.org/2/library/csv.html?highlight=csv#csv.reader

csvfile can be any object which supports the iterator protocol and returns a string each time its next() method is called

csvfile 可以是任何支持迭代器协议并在每次调用 next() 方法时返回一个字符串的对象

Thus, a StringIO.StringIO(), str.splitlines()or even a generator are all good.

因此,一个StringIO.StringIO()str.splitlines()甚至一个生成器都是好的。

回答by chfw

Here's an alternative solution:

这是一个替代解决方案:

>>> import pyexcel as pe
>>> text="""1,2,3
... a,b,c
... d,e,f"""
>>> s = pe.load_from_memory('csv', text)
>>> s
Sheet Name: csv
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| a | b | c |
+---+---+---+
| d | e | f |
+---+---+---+
>>> s.to_array()
[[u'1', u'2', u'3'], [u'a', u'b', u'c'], [u'd', u'e', u'f']]

Here's the documentation

这是文档

回答by JimS

Use this to have a csv loaded into a list

使用它可以将 csv 加载到列表中

import csv

csvfile = open(myfile, 'r')
reader = csv.reader(csvfile, delimiter='\t')
my_list = list(reader)
print my_list
>>>[['1st_line', '0'],
    ['2nd_line', '0']]

回答by soulmachine

The official doc for csv.reader()https://docs.python.org/2/library/csv.htmlis very helpful, which says

csv.reader()https://docs.python.org/2/library/csv.html的官方文档 非常有帮助,它说

file objects and list objects are both suitable

文件对象和列表对象都适合

import csv

text = """1,2,3
a,b,c
d,e,f"""

lines = text.splitlines()
reader = csv.reader(lines, delimiter=',')
for row in reader:
    print('\t'.join(row))

回答by roundar

Per the documentation:

根据文档:

And while the module doesn't directly support parsing strings, it can easily be done:

虽然该模块不直接支持解析字符串,但可以轻松完成:

import csv
for row in csv.reader(['one,two,three']):
    print row

Just turn your string into a single element list.

只需将您的字符串转换为单个元素列表。

Importing StringIO seems a bit excessive to me when this example is explicitly in the docs.

当这个示例明确出现在文档中时,导入 StringIO 对我来说似乎有点过分。

回答by webbyfox

Panda is quite powerful and smart library reading CSV in Python

Panda 是一个非常强大和智能的库,可以在 Python 中读取 CSV

A simple example here, I have example.zip file with four files in it.

这里有一个简单的例子,我有一个包含四个文件的 example.zip 文件。

EXAMPLE.zip
 -- example1.csv
 -- example1.txt
 -- example2.csv
 -- example2.txt


from zipfile import ZipFile
import pandas as pd


filepath = 'EXAMPLE.zip'
file_prefix = filepath[:-4].lower()

zipfile = ZipFile(filepath)
target_file = ''.join([file_prefix, '/', file_prefix, 1 , '.csv'])

df = pd.read_csv(zipfile.open(target_file))

print(df.head()) # print first five row of csv
print(df[COL_NAME]) # fetch the col_name data

Once you have data you can manipulate to play with a list or other formats.

获得数据后,您可以操作以使用列表或其他格式进行播放。