Python csv字符串转数组

发布于 2024-09-11 13:27:59 字数 174 浏览 4 评论 0原文

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

我不认为我想要内置 csv 模块 因为在所有示例中我'我们已经看到它需要文件路径,而不是字符串。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

美胚控场 2024-09-18 13:27:59

您可以使用 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))

更简单的版本,在换行符上使用 split()

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

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

Python 2 上,您必须导入 StringIO 作为

from StringIO import StringIO

替代。

You can convert a string to a file object using io.StringIO and then pass that to the csv module:

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:

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 \n as separator, and then split() each line into values, but this way you must be aware of quoting, so using csv module is preferred.

On Python 2 you have to import StringIO as

from StringIO import StringIO

instead.

音盲 2024-09-18 13:27:59

简单 - 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']]

Simple - the csv module works with lists, too:

>>> 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']]
无声静候 2024-09-18 13:27:59

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

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

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))

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

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))
小ぇ时光︴ 2024-09-18 13:27:59

根据文档:

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

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

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

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

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.

魔法少女 2024-09-18 13:27:59

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

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

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

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.

The Python documentation for the csv module implements 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:

with open("some.csv", "rb") as csvFile: 
    for row in UnicodeReader(csvFile, encoding="iso-8859-15"):
        print row
空气里的味道 2024-09-18 13:27:59

不是通用的 CSV 解析器,但可用于带有逗号的简单字符串。

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

要解析 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

Not a generic CSV parser but usable for simple strings with commas.

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

To parse a CSV file:

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
错々过的事 2024-09-18 13:27:59

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

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

因此,一个 StringIO.StringIO(), str.splitlines() 甚至发电机都很好。

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

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

赢得她心 2024-09-18 13:27:59

使用它可以将 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']]

Use this to have a csv loaded into a list

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']]
鱼窥荷 2024-09-18 13:27:59

这是一个替代解决方案:

>>> 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 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

趁年轻赶紧闹 2024-09-18 13:27:59

对于仍在寻找将标准 CSV str 转换为 list[str] 以及反向转换的可靠方法的人,这里有两个函数,我从一些此线程和其他线程中的答案:

def to_line(row: list[str]) -> str:
    with StringIO() as line:
        csv.writer(line).writerow(row)
        return line.getvalue().strip()


def from_line(line: str) -> list[str]:
    return next(csv.reader([line]))

For anyone still looking for a reliable way of converting a standard CSV str to a list[str] as well as in reverse, here are two functions I put together from some of the answers in this and other SO threads:

def to_line(row: list[str]) -> str:
    with StringIO() as line:
        csv.writer(line).writerow(row)
        return line.getvalue().strip()


def from_line(line: str) -> list[str]:
    return next(csv.reader([line]))
欲拥i 2024-09-18 13:27:59

对于 csv 文件:

data = blob.download_as_text()

pd.DataFrame(i.split(",") for i in data.split("\n"))

For csv files:

data = blob.download_as_text()

pd.DataFrame(i.split(",") for i in data.split("\n"))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文