如何读取 json 内带有分隔符的 csv?
How to read csv with separator inside json?
我想读取 csv 文件,但分隔符有时会出现在第二列 (json)。当它出现在引号内时是否可以转义管道?
from io import StringIO
import pandas as pd
data = 'col1|{"a":"1","b":"2|3","c":"4"}'
df = pd.read_csv(
StringIO(data),
header=None,
sep='|',
quoting=csv.QUOTE_NONE,
quotechar='"',
doublequote=False
)
当前
0
1
2
col1
{"a":"1","b":"2
3","c":"4"}
预计
0
1
col1
{"a":"1","b":"2|3","c":"4"}
试试这个:
data = """col1|'{"a":"1","b":"2|3","c":"4"}'"""
df = pd.read_csv(
StringIO(data),
header=None,
sep='|',
quotechar="'"
)
pandas如果值被包含在指定的quotechar
中,则可以将值识别为整个字符串,因为您需要将json这样的字符串包围在[=12中=].
另外我triple-quoted data
字符串保留单引号。
您还可以在 csv 中编写一个具有相同字符串的简单文件,并尝试 read_csv 和 quotechar = "'"
我想读取 csv 文件,但分隔符有时会出现在第二列 (json)。当它出现在引号内时是否可以转义管道?
from io import StringIO
import pandas as pd
data = 'col1|{"a":"1","b":"2|3","c":"4"}'
df = pd.read_csv(
StringIO(data),
header=None,
sep='|',
quoting=csv.QUOTE_NONE,
quotechar='"',
doublequote=False
)
当前
0 | 1 | 2 |
---|---|---|
col1 | {"a":"1","b":"2 | 3","c":"4"} |
预计
0 | 1 |
---|---|
col1 | {"a":"1","b":"2|3","c":"4"} |
试试这个:
data = """col1|'{"a":"1","b":"2|3","c":"4"}'"""
df = pd.read_csv(
StringIO(data),
header=None,
sep='|',
quotechar="'"
)
pandas如果值被包含在指定的quotechar
中,则可以将值识别为整个字符串,因为您需要将json这样的字符串包围在[=12中=].
另外我triple-quoted data
字符串保留单引号。
您还可以在 csv 中编写一个具有相同字符串的简单文件,并尝试 read_csv 和 quotechar = "'"