Converting a Text file to JSON format using Python
You can read it as a csv
file and convert it into json
. But, be careful with spaces as you've used it as separator, the values with spaces should be carefully handled. Otherwise, if possible make the separator ,
instead of space
.
the working code for what you're trying,
import csv
import json
with open('file.txt', 'rb') as csvfile:
filereader = csv.reader(csvfile, delimiter=' ')
i = 0
header = []
out_data = []
for row in filereader:
row = [elem for elem in row if elem]
if i == 0:
i += 1
header = row
else:
row[0:2] = [row[0]+" "+row[1]]
_dict = {}
for elem, header_elem in zip(row, header):
_dict[header_elem] = elem
out_data.append(_dict)
print json.dumps(out_data)
output,
[
{
"Source":"B cells",
"Target":"Streptococcus",
"Value":"pneumoniae"
},
{
"Source":"B cells",
"Target":"Candida",
"Value":"albicans"
},
{
"Source":"B cells",
"Target":"Mycoplasma",
"Value":"120"
},
{
"Source":"B cells",
"Target":"Neisseria",
"Value":"111"
},
{
"Source":"B cells",
"Target":"Pseudomonas",
"Value":"aeruginosa"
}
]
UPDATE: Just noticed your updated question with json
sample that you require. Hope, you could build it with the above example I've written.
Text file to JSON
Just use the line contents, do not use zip
:
import json
result = []
with open('sample.txt', 'r') as f_in:
for line in f_in:
line = line.strip()
if not line:
continue # skip empty lines
result.append({'x': line, 'y': 'a'})
with open('formated.json', 'w') as f_out:
print(json.dumps(result, indent=2))
#f_out.write(json.dumps(result, indent=2))
Out:
[
{
"x": "1234",
"y": "a"
},
{
"x": "5678",
"y": "a"
},
{
"x": "9765",
"y": "a"
}
]
Converting a text file to JSON using Python
You can read each line, split it with ':'
and build a dictionary with these two values. Then append the dictionary to a list. Finally you can use json.dumps
to convert your Python object(list) to a string which is in JSON format.
import json
res = []
with open('<Path to file.txt>') as f:
for line in f:
kid, hex_key = line.strip().split(':')
res.append({'kid': kid, 'hex_key': hex_key})
print(json.dumps(res))
output:
[{"kid": "b770d5b4bb6b594daf985845aae9aa5f", "hex_key": "b0cb46d2d31cf044bc73db71e9865f6f"}, {"kid": "a6cde1a737b44799ad78f1c1df3bcc2d", "hex_key": "77aa72de248cf3649d052a49995c0c84"}, {"kid": "555040e976be48cebea579309f4e6339", "hex_key": "2b0003ca52ffc309ef76872c5ab3729c"}]
convert text to json with this format
Assuming you got the initial dict:
images = [{u'image_id': 0, u'caption': u'the man is playing a guitar'},
{u'image_id': 1, u'caption': u'a man is playing a guitar'},
{u'image_id': 2, u'caption': u'a woman is slicing cucumbers'},
{u'image_id': 3, u'caption': u'the woman is slicing cucumbers'},
{u'image_id': 4, u'caption': u'a woman is cutting cucumbers'}]
We can simply define the datasetRES
object as :
datasetRES = {'annotations': images}
Now you can use the following code:
imgToAnnsRES = {ann['image_id']: [] for ann in datasetRES['annotations']}
convert content of text file to json in python
json_data = {}
with open("json_content.txt","r") as fp:
for line in fp.readlines():
if line != "\n":
split_data = line.split(':',)
print(split_data)
json_data[split_data[0]] = split_data[1].split('\n')[0]
print(json_data)
Output:
{'hostname': 'WGNAIOP', 'fsystem': '/dev/sdb', 'actual_size': '2.5T', 'used_disk': '609G', 'avail_disk': '1.7T', 'percentage': '26%', 'mount_disk': '/data'}
Convert txt file into JSON in python
There's no simple way in the standard library to convert that data format to JSON, so we need to write a parser. However, since the data format is fairly simple that's not hard to do. We can use the standard csv
module to read the data. The csv.reader
will handle the details of parsing spaces and quoted strings correctly. A quoted string will be treated as a single token, tokens consisting of a single word may be quoted but they don't need to be.
The csv.reader
normally gets its data from an open file, but it's quite versatile, and will also read its data from a list of strings. This is convenient while testing since we can embed our input data into the script.
We parse the data into a nested dictionary. A simple way to keep track of the nesting is to use a stack, and we can use a plain list as our stack.
The code below assumes that input lines can be one of three forms:
- Plain data. The line consists of a key - value pair, separated by at least one space.
- A new subobject. The line starts with a key and ends in an open brace
{
. - The end of the current subobject. The line contains a single close brace
}
import csv
import json
raw = '''\
name {
first_name: "random"
}
addresses {
location {
locality: "India"
street_address: "xyz"
postal_code: "300092"
full_address: "street 1 , abc,India"
}
}
projects {
url: "www.githib.com"
}
'''.splitlines()
# A stack to hold the parsed objects
stack = [{}]
reader = csv.reader(raw, delimiter=' ', skipinitialspace=True)
for row in reader:
#print(row)
key = row[0]
if key == '}':
# The end of the current object
stack.pop()
continue
val = row[-1]
if val == '{':
# A new subobject
stack[-1][key] = d = {}
stack.append(d)
else:
# A line of plain data
stack[-1][key] = val
# Convert to JSON
out = json.dumps(stack[0], indent=4)
print(out)
output
{
"name": {
"first_name:": "random"
},
"addresses": {
"location": {
"locality:": "India",
"street_address:": "xyz",
"postal_code:": "300092",
"full_address:": "street 1 , abc,India"
}
},
"projects": {
"url:": "www.githib.com"
}
}
How to convert a text file in a set line range to json format by Python
First of all, never post screenshots, use the editor to type out the text.
You have to edit based on your requirements, it makes some assumptions based on your sample.
sample_input.txt
## SASADA
# RANDOM
XXXX
[server]
server_id = 1
port = 8000
[client]
port = 8001
code.py
all_lines = open('sample_input.txt', 'r').readlines() # reads all the lines from the text file
# skip lines, look for patterns here []
final_dict = {}
server = 0 # not yet found server
for line in all_lines:
if '[server]' in line:
final_dict['server'] = {}
server = 1
if '[client]' in line:
final_dict['client'] = {}
server = 2
if server == 1:
try:
clean_line = line.strip() # get rid of empty space
k = clean_line.split('=')[0] # get the key
v = clean_line.split('=')[1]
final_dict['server'][k] = v
except:
passs
if server == 2:
# add try except too
clean_line = line.strip() # get rid of empty space
k = clean_line.split('=')[0] # get the key
v = clean_line.split('=')[1]
final_dict['client'][k] = v
Related Topics
Check If File Has a CSV Format With Python
Cast String to Float Is Not Supported in Linear Model
List of the Most Recently Updated Files in Python
Python Sockets Multiple Messages on Same Connection
How Does the Code Prints 1 2 6 24 as Output and Not 24 6 2 1
How to Hide Chrome Driver in Python
How to Normalize a 2-Dimensional Numpy Array in Python Less Verbose
Find First Non-Zero Value in Each Column of Pandas Dataframe
Codehs Python, Remove All from String
How to Print Numbers in a List That Are Less Than a Variable. Python
Pyspark - Pass List as Parameter to Udf
Get Current Url from Browser Using Python
How to Convert a 16-Bit to an 8-Bit Image in Opencv
Finding the Two Closest Numbers in a List Using Sorting
Vscode: There Is No Pip Installer Available in the Selected Environment
How to Calculate R-Squared Using Python and Numpy