我正在读取一个csv文件,并尝试根据逗号分割其行。在本例中,这里的行包含一些值,这些值的一部分是逗号,而该值以{}开头和结尾。
我的分割功能:
def process(self, row):
"""
Splits each row on commas
"""
Uid, controlNo, profileType, LAStpointDetail, LastPointDate =
row.split(",")
我的行示例:
0923,41003,Permanent,{""Details"": [{""data"": {""status"": ""FAILURE"", ""id"": ""12345""}, ""DetailType"": ""Existing""}]},2019-06-27
在行中如果看到“ LAStpointDetail”,它已经包含多个逗号。如何根据逗号分割整行。
答案 0 :(得分:1)
不可能确切说明问题中的数据是如何被引用的(最好粘贴行的repr),但让我们假设它是这样的:
'"0923","41003","Permanent","{""Details"": [{""data"": {""status"": ""FAILURE"", ""id"": ""12345""}, ""DetailType"": ""Existing""}]}","2019-06-27"'
如果是这样,并且您有一个包含多行数据的文件,则可以使用csv模块读取它:
import csv
import json
with open('myfile.csv', 'rb') as f:
reader = csv.reader(f)
# Remove the next line if the first row is not the headers.
# next(reader) # Skip header row.
for row in reader:
Uid, controlNo, profileType, LAStpointDetail, LastPointDate = row
# Load embedded json into a dictionary.
detail_dict = json.loads(LAStpointDetail)
# Do something with these values.
如果只有一行作为字符串,则仍然可以使用csv模块:
>>> row = '"0923","41003","Permanent","{""Details"": [{""data"": {""status"": ""FAILURE"", ""id"": ""12345""}, ""DetailType"": ""Existing""}]}","2019-06-27"'
>>> # Make the data an element in a list ([]).
>>> reader = csv.reader([row])
>>> Uid, controlNo, profileType, LAStpointDetail, LastPointDate = next(reader)
>>> print Uid
0923
>>> d = json.loads(LAStpointDetail)
>>> d
{u'Details': [{u'DetailType': u'Existing', u'data': {u'status': u'FAILURE', u'id': u'12345'}}]}
>>> print d['Details'][0]['data']['status']
FAILURE