parsing 3 lists into list of dictionaries in python

yulGM

I'm parsing a csv line by line. There are 3 columns in a row that need to be each split into lists and then combined into dictionaries in a list. The data inside each column is separated by two pipes: ||

incoming data:

buttons, clicked, id
choice 1 || choice 2 || choice 3, TRUE || FALSE || FALSE, choice 1 id|| choice 2 id || choice 3 id

enter image description here

  • the data was already imported via csv DiskReader and each row in the script is ordered dictionary and looks like:
('buttons','choice 1 || choice 2 || choice 3'),
('clicked', 'TRUE || FALSE || FALSE'),
('id','choice 1 id|| choice 2 id || choice 3 id'),

EDITED TO ADD:
input has a lot more columns that should not be included. only these 3 columns are included for this step.

i.e. sample output of a row that is being processed

print(data[0])

OrderedDict([
('pathId', 'test_id'),
('stepId', ''),
('nodeId', 'ROOT'),
('responseId', 'test_response'),
('responseUuid', ''),
('type', ''),
('language', 'en-US'),
('buttons','choice 1 || choice 2 || choice 3'),
('clicked', 'TRUE || FALSE || FALSE'),
('id','choice 1 id|| choice 2 id || choice 3 id'),
('state', 'resolved'),
('flags', 'accepted')])

the actual number of columns is dynamic and unpredictable. But in rows that contain values for buttons, clicked and id will need this processing.
END EDIT

Expected output for this row needs to be:

buttonChoices =
[
    {
        "button": "choice 1",
        "clicked": true,
        "id": "choice 1 id"
    },
    {
        "button": "choice 2",
        "clicked": false,
        "id": "choice 2 id"
    },
    {
        "button": "choice 3",
        "clicked": false,
        "id": "choice 3 id"
    }
]
  • i don't know in advance how many values will be in the list, but it will be same in these 3 columns

For now I have:

for row in data:
    buttonChoices = []
    buttonText = row['button'].split('||')
    buttonClicked = row['clicked'].split('||')
    buttonId = row['id'].split('||')

but stuck for next step

Alain T.

You can use zip to combine the values 3 by 3 and to associate them to a key:

data = dict([ ('buttons','choice 1 || choice 2 || choice 3'),
              ('clicked', 'TRUE || FALSE || FALSE'),
              ('id','choice 1 id|| choice 2 id || choice 3 id')])

buttonChoices = [ dict(zip(data,map(str.strip,values)))
                  for values in zip(*(v.split("||") for v in data.values())) ]

print(buttonChoices)
[{'buttons': 'choice 1', 'clicked': 'TRUE', 'id': 'choice 1 id'},
 {'buttons': 'choice 2', 'clicked': 'FALSE', 'id': 'choice 2 id'},
 {'buttons': 'choice 3', 'clicked': 'FALSE', 'id': 'choice 3 id'}]

note: I added a map(str.strip,...) in there to clean up the messy string separations but you don't need it if your actual data is properly formatted

To generalize this a bit, you can define input keys and output keys to filter and rename as needed:

inKeys  = ('buttons','clicked','id')
outKeys = ('button','clicked','id')
buttonChoices = [ dict(zip(outKeys,map(str.strip,values)))
                  for values in zip(*(data[k].split("||") for k in inKeys)) ]

print(buttonChoices)
[{'button': 'choice 1', 'clicked': 'TRUE', 'id': 'choice 1 id'},
 {'button': 'choice 2', 'clicked': 'FALSE', 'id': 'choice 2 id'},
 {'button': 'choice 3', 'clicked': 'FALSE', 'id': 'choice 3 id'}]

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

How to quickly convert from items in a list of lists to list of dictionaries in python?

Python: Dictionary with list of dictionaries

Python list to list of lists

Iterate through list of dictionaries in python

Python: How to copy a list of a dictionaries

Python - Error , iterating list of dictionaries

Python - Traverse list of dictionaries - Error as NoneType

Problems removing dictionaries from list in Python

Apply function to each element in 3 lists of tuples to get 1 resultant list python3

Looping through list of lists in Python

Dictionaries and loops in Python 3.x

Divide list and append the list to separate lists python

Converting 4 dimensional tensors into list of lists of lists (Python)

Structured data, dictionaries inside lists

Python sort list of dicts with lists inside

Python reordering the list of lists after sorting

Find compatible lists in a bigger list with Python

Python - How to delete the last element in a list of lists?

Filter a list of dictionaries by keys with different value for each key in Python

Python: How to create a csv string (no file) from a list of dictionaries?

How to sort a large list of dictionaries without loading into memory in Python

Python Group and aggregate unidirectionally a list of dictionaries by multiple keys

Oddity in how Python3 handles lists

How to convert a list of lists of lists into a list of lists?

Python-Beautiful Soup not parsing entire unordered list

Python Defining Lists in Lists

In Python, is there a way to sort a list made of lists and tuples, consistently?

Equalize the value of two lists and return one list in Python?

Printing out dictionaries in Python

TOP 리스트

  1. 1

    Ionic 2 로더가 적시에 표시되지 않음

  2. 2

    JSoup javax.net.ssl.SSLHandshakeException : <url>과 일치하는 주체 대체 DNS 이름이 없습니다.

  3. 3

    std :: regex의 일관성없는 동작

  4. 4

    Xcode10 유효성 검사 : 이미지에 투명성이 없지만 여전히 수락되지 않습니까?

  5. 5

    java.lang.UnsatisfiedLinkError : 지정된 모듈을 찾을 수 없습니다

  6. 6

    rclone으로 원격 디렉토리의 모든 파일을 삭제하는 방법은 무엇입니까?

  7. 7

    상황에 맞는 메뉴 색상

  8. 8

    SMTPException : 전송 연결에서 데이터를 읽을 수 없음 : net_io_connectionclosed

  9. 9

    정점 셰이더에서 카메라에서 개체까지의 XY 거리

  10. 10

    Windows cmd를 통해 Anaconda 환경에서 Python 스크립트 실행

  11. 11

    다음 컨트롤이 추가되었지만 사용할 수 없습니다.

  12. 12

    C #에서 'System.DBNull'형식의 개체를 'System.String'형식으로 캐스팅 할 수 없습니다.

  13. 13

    JNDI를 사용하여 Spring Boot에서 다중 데이터 소스 구성

  14. 14

    Cassandra에서 버전이 지정된 계층의 효율적인 모델링

  15. 15

    복사 / 붙여 넣기 비활성화

  16. 16

    Android Kotlin은 다른 활동에서 함수를 호출합니다.

  17. 17

    Google Play Console에서 '예기치 않은 오류가 발생했습니다. 나중에 다시 시도해주세요. (7100000)'오류를 수정하는 방법은 무엇입니까?

  18. 18

    SQL Server-현명한 데이터 문제 받기

  19. 19

    Seaborn에서 축 제목 숨기기

  20. 20

    ArrayBufferLike의 typescript 정의의 깊은 의미

  21. 21

    Kubernetes Horizontal Pod Autoscaler (HPA) 테스트

뜨겁다태그

보관