I'm parsing a json list and printing X values in a txt file. The problem is that I need to exclude specific values and by doing so leaves empty spaces in the list like so:
CfZzroU5Zg3EEnmSdnURwu49XAdsUzJLqftAMsB6enQA
CJgzYGNSr1sioRituTStFkndugx4GrKv88r9XkbgNVh3
HJ1cZjtp9zxYAMNuTGvRB5inpmXuWVdoZy6RTWAF2vzf
9BTKTYTAyu3tEVWXHkQ155Er2ZhwxzQecePVpdyHCV5o
BCPt8Wc1TEKKw1M1WcnBWofKfKLBnapUXPyuX7EmqifY
6sGj9in9BCciX5D17EZsW4btfoTBWDx8AVR63ooUunHM
I want to remove those newlines but the only way I could find to do this with end=''
basically removes everything including newlines, ending up with something like this:
PGoymXwWGHaF4ApyZLjcGRFvvFWVHZdDTdgkndg2WgEyspE9ik8Ay2ehyKRtntEY2tvQojQfj1rqrH5Ph91RSHqcRdWaayHug3QpcyuoctLkxcx2Sq3N2dmWcywzeHwL9uX2MJyJcE6HY9ZqNedVzUGUwmhZc6B7aSiCjs3UjohjYVoNX6orGqgFGCanq1NETb6r4ostLE96VGPchFGCq2ZRRepz4vZSTEdrVDrztPxqGRXqT9fHpkjjwkg1WM6k7kieSxrgakCgnAkkXB2s9wzixVzThdLKSp6qK5DJoDemTEhAHDix5n1ZSKiXhBNZ7rEP8AGByNL3XnNB5wJC7655VLgyYykC5ybdjpDzEEVnVpkvcFKqqstXyiA3jMdJiXCmXrz8iG5M1fXKhLhnG6CprKs71SJRHTyxhupdKi2Asatjbwh1mP9yXiQ8Q35AtH7MyRmmGifZhMsBCyKiHZqeSou9tZeN3f9ocqXi1792nBgiHi192q66wqJkeEPw6XpX2NJpii9qgDBzv1q7zjfWXEC6MP8pD8SSjid15q4uhNTBN9zx5i4dMimw61F4smh4wEVnVpkvcFKqqstXyiA3jMdJiXCmXrz8iG5M1fXKhLhnGHYVs2auZ3CvTy9GxgYZVYncJWEy5HHBHNYzoB5722zWbDLddoiBzRS6217u7gWuuWeFesLJbYVC7xETF3KjkSXTTFNZxe9FWdYCJVUdBpSpd4Quk4kG4SbGYDgcchkfxR72xCWkJA66uNbVdmUKmrmbdhdds2yxVnovVtzr8tqHvDKLM8FEZaEFxXqUwdtsGovSaJamty2kAz16ERTawdt8Bz8ZXGkEqvw25KQ6rZRHxyUeCJoJSV8Z47DCGNzR1bFE1pGLpFNZxe9FWdYCJVUdBpSpd4Quk4kG4SbGYDgcchkfxR72x3PkbfxGy1W6eSLfjwPuf1rYbst4GKkN2jBiz1jjZJfGM94UHpi9NnLjrnhGqzmE5dA7dSvgMewUjxorwzf2wJ6DV9J1xhdvqdf6ij9AWa3anGeP8iW9Htb32z2k3KLuxL2LV94UHpi9NnLjrnhGqzmE5dA7dSvgMewUjxorwzf2wJ6DV6gZRvYWEaXAVchYDvixUFiECS3UmQ3Cx65nYdt9DBVvtDueEQ2A3SsPKdFT5BYPS9AgWB6b7horZJuo5DCpRRjQL94UHpi9NnLjrnhGqzmE5dA7dSvgMewUjxorwzf2wJ6DV94UHpi9NnLjrnhGqzmE5dA7dSvgMewUjxorwzf2wJ6DVAFa
This is the code I'm using:
import json
with open('file.json') as json_file:
data = json.load(json_file)
exclude = ['FFFFFFFFFFFFFFFFFFFFFFFFFFFF', 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBB', 'AAAAAAAAAAAAAAAAAAAAAAAAA', 'ZZZZZZZZZZZZZZZZZZZZZZZZZZZZ']
with open("output.txt", "a") as f:
for i in range(len(data)):
if data[i]['owner'] in exclude:
data[i]['owner'] = ''
print (data[i]['owner'], end='', file=f)
Any help would be greatly appreciated! Thanks
It looks like regular print statements should work with an else
condition:
with open("output.txt", "a") as f:
for i in range(len(data)):
if data[i]['owner'] in exclude:
data[i]['owner'] = ''
else
print (data[i]['owner'], file=f)
You will need to skip some iterations in order to skip the unwanted values. And you can still use '\n' as a delimiter!
with open("output.txt", "a") as f:
for i in range(len(data)):
if data[i]['owner'] in exclude:
continue
print (data[i]['owner'], end='\n', file=f)
I would just suggest to not print anything in case of the string is one that should be excluded.
with open("output.txt", "a") as f:
for entry in data:
if entry['owner'] in exclude:
continue
print (entry['owner'], file=f)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.