Home > other >  Combining multiple line items - from a list, into one single row with delimiter using Python
Combining multiple line items - from a list, into one single row with delimiter using Python

Time:10-10

Not 100% sure on the correct terminology, but I have the following hashes in Python: I'm trying to put them all in a single row separated by |. So far I've tried join(), but that just splits each character for example d|1|5|5 etc for the first line. Any suggestions?

d155dd4712a53d94ddb7916da78e15762bb9ee55
11a50ecbd959d839323717811e0f687298448042
09a52c6b05159edd5edf5be58e16c58ed286c16e
0348395f05e7ec1db81a3bf1e65d0a3117f2ed1a
db578880bcc20ce89dc91e3ad445057af25f25e1
e7da2f446abd104a62f96213d45257dfb6536e9e
f6d8142f5bdfe6042fc5e0044a668d658592e0d2

This is my input & what I have so far:

import requests
import jq

url = 'http://192.168.1.224:8080/api/v2/torrents/info?filter=errored&category=test'

torrentListParsed = requests.get(url).json()

finalList = (jq.compile(".[] | .hash").input(torrentListParsed).text())

# With quotation marks:
# "d155dd4712a53d94ddb7916da78e15762bb9ee55"
# "11a50ecbd959d839323717811e0f687298448042"
# "09a52c6b05159edd5edf5be58e16c58ed286c16e"
# "0348395f05e7ec1db81a3bf1e65d0a3117f2ed1a"
# "db578880bcc20ce89dc91e3ad445057af25f25e1"
# "e7da2f446abd104a62f96213d45257dfb6536e9e"
# "f6d8142f5bdfe6042fc5e0044a668d658592e0d2"

finalListStripped = finalList.replace('"', '')
print(finalListStripped)

# Without quotation marks:
# d155dd4712a53d94ddb7916da78e15762bb9ee55
# 11a50ecbd959d839323717811e0f687298448042
# 09a52c6b05159edd5edf5be58e16c58ed286c16e
# 0348395f05e7ec1db81a3bf1e65d0a3117f2ed1a
# db578880bcc20ce89dc91e3ad445057af25f25e1
# e7da2f446abd104a62f96213d45257dfb6536e9e
# f6d8142f5bdfe6042fc5e0044a668d658592e0d2

CodePudding user response:

You have to make a list with all the hashes for join to do what you want.

hashes = [
    "d155dd4712a53d94ddb7916da78e15762bb9ee55",
    "11a50ecbd959d839323717811e0f687298448042",
    "09a52c6b05159edd5edf5be58e16c58ed286c16e",
    "0348395f05e7ec1db81a3bf1e65d0a3117f2ed1a",
    "db578880bcc20ce89dc91e3ad445057af25f25e1",
    "e7da2f446abd104a62f96213d45257dfb6536e9e",
    "f6d8142f5bdfe6042fc5e0044a668d658592e0d2"
]

string = "|".join(hashes)

To get that list, just split finalListStripped, which is a string and not a list.

hashes = finalListStripped.split()
  • Related