read_csv读取\,作为分隔符 [英] read_csv reads \, as a separator

查看:736
本文介绍了read_csv读取\,作为分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道read_csv()使用逗号(,)作为分隔符,但是我有一个文件,其某些单元格的内容中包含逗号.

I know that read_csv() uses comma (,) as separator but I have a file which some of its cells has comma in their content.

在该文件中,作者使用反斜杠逗号(\,)表示此逗号不是分隔符.

In that file author used backslash comma (\,) to show that this comma is not a separator.

但是当我用read_csv()读取文件时,它将所有逗号都视为分隔符.这是csv文件中的示例行:

But when I read the file with read_csv(), it consider all commas as separator. Here is a sample row in csv file:

346882588,206801833,1049600263,Dzianis Dzenisiuk,5,StuckPixel\, Inc.,Feb 11\, 2010,2,3,1265846400

我知道应该有n列,因此我逐行读取csv文件并删除多余的逗号和反斜杠逗号.

I know that there should be n columns so I read csv file line by line and remove extra commas and backslash commas.

但是应该有更好的方法.

But there should be better way.

推荐答案

您需要将反斜杠配置为转义字符,并使用

You need to configure the backslash as an escape character, with the escapechar option:

pandas.read_csv(fileobj_or_filename, escapechar='\\')

演示:

>>> import pandas, csv
>>> from io import StringIO
>>> f = StringIO(r'''346882588,206801833,1049600263,Dzianis Dzenisiuk,5,StuckPixel\, Inc.,Feb 11\, 2010,2,3,1265846400
... ''')
>>> df = pandas.read_csv(f, names='abcdefghij', escapechar='\\')
>>> df['f']
0    StuckPixel, Inc.
Name: f, dtype: object
>>> df['g']
0    Feb 11, 2010
Name: g, dtype: object

这篇关于read_csv读取\,作为分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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