Have a txt with contents :
{
hello : 1,two:three,four:five,six:seven,
}
how to remove the last , in the above string ?
while using them as dictionaries for further. it cant be parsed because of the last delimiter.
code :
import json
d2=json.load(open(test.txt))
i cant change the source code. coz i am extracting data from a json file(json.dump) and creating a new json. is there any way of doing that other than dump/changing the source code
This removes the last , in your string without the need of any further import's.
with open('test.txt', 'r') as f:
s = f.read()
s = s[::-1].replace(',', '', 1)[::-1]
the output of s is then:
{
hello : 1,two:three,four:five,six:seven
}
Simple replace should work fine.
broken_json = '''{
hello : 1,two:three,four:five,six:seven,
bye : 42,ick:poo,zoo:bar,}'''
j = broken_json.replace(',}', '}').replace(',\n}','\n}')
The result at this point is still not valid JSON, because the dictionary keys need to be quoted; but this is outside the scope of your question so I will not try to tackle that part.
string ='{hello : 1,two:three,four:five,six:seven,}'
length = len(string)
for i in range(length):
if(string[i] == ','):
string2 = string[0:i] + string[i + 1:length]
print (string2)
The output type would be a string. So, convert it to a dict later.
import re
string ='{hello : 1,two:three,four:five,six:seven,}'
match = re.search(r'(.*),[^,]*$', string)
print (match.group(1)+"}")
Try this #selcuk for an efficient one
Related
I am trying to write data from json file to CSV file using python. My code is like this:
CSVFile1 = open('Group_A_participant_1_1.csv', 'a')
writeCSV1 = csv.writer(CSVFile1)
for file in data['annotations'][3]['instances']:
var = file['arguments'].get('argument1')
writeCSV1.writerow(var)
CSVFile.close()
My output is:
So my problem is that I can not see the whole word in one cell.
Thanks your helps inn advance!
I expect to get each word in one single cell.
Change
writeCSV1.writerow(var)
to
writeCSV1.writerow([var])
so you're writing an one-item list with your var instead of having the CSV module interpret var, a string, as separate characters.
For instance:
import csv
import sys
writeCSV1 = csv.writer(sys.stdout)
data = {
"annotations": [
{},
{},
{},
{
"instances": [
{"arguments": {"argument1": "foo"}},
{"arguments": {"argument1": "bar"}},
]
},
],
}
for file in data["annotations"][3]["instances"]:
var = file["arguments"].get("argument1")
writeCSV1.writerow([var])
prints out
foo
bar
whereas taking the brackets out from around [var] results in
f,o,o
b,a,r
as you described.
Click on the first cell of the column where you want the converted
names to appear (B2).
Type equal sign (=), followed by the text “Prof. “, followed by an
ampersand (&).
Select the cell containing the first name (A2).
Press the Return Key.
You will notice that the title “Prof.” is added before the first name in the list.
My Code:
with open('music_queue.json', 'r') as f:
data = f.read()
list_str = data.split('\n')
print(list_str)
db = []
for d in list_str:
db.append(json.loads(d))
My raw JSON:
{"guild_id" : 00000, "song_list" : []}
I have tried doing:
data = data.replace('\"', '\\\"')
only for me to have this error:
Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
I've been at this for hours. What's going on?
Also, apologies if this has already been answered. I literally couldn't find anything here that I haven't tried already.
Text inside json files must follow JSON standard and 00000 (without quotes to be marked as string) is not a valid value - replace it with 0 or "00000".
When you open a valid json file you can load the contents straight into a dictionary, like this:
with open('music_queue.json', 'r') as f:
data = json.load(f)
Example of valid json file:
{"guild_id" : 10000, "song_list" : []}
P.S. You have to use double quotes "" inside json files instead of single quotes ''
Expecting property name enclosed in double quotes
Let's understand this error, it simply says the property name has to be in double quotes. Now this refers to your JSON file since 00000 is not valid. If it was a number 0 would have been enough. But having 4 zeros is making it read it as a string
Strings in json need to be enclosed in double quotes
Change the json to:
{"guild_id" : "00000", "song_list" : []}
Hope it helps!
i have following string in python
b'{"personId":"65a83de6-b512-4410-81d2-ada57f18112a","persistedFaceIds":["792b31df-403f-4378-911b-8c06c06be8fa"],"name":"waqas"}'
I want to print the all alphabet next to keyword "name" such that my output should be
waqas
Note the waqas can be changed to any number so i want print any name next to keyword name using string operation or regex?
First you need to decode the string since it is binary b. Then use literal eval to make the dictionary, then you can access by key
>>> s = b'{"personId":"65a83de6-b512-4410-81d2-ada57f18112a","persistedFaceIds":["792b31df-403f-4378-911b-8c06c06be8fa"],"name":"waqas"}'
>>> import ast
>>> ast.literal_eval(s.decode())['name']
'waqas'
It is likely you should be reading your data into your program in a different manner than you are doing now.
If I assume your data is inside a JSON file, try something like the following, using the built-in json module:
import json
with open(filename) as fp:
data = json.load(fp)
print(data['name'])
if you want a more algorithmic way to extract the value of name:
s = b'{"personId":"65a83de6-b512-4410-81d2-ada57f18112a",\
"persistedFaceIds":["792b31df-403f-4378-911b-8c06c06be8fa"],\
"name":"waqas"}'
s = s.decode("utf-8")
key = '"name":"'
start = s.find(key) + len(key)
stop = s.find('"', start + 1)
extracted_string = s[start : stop]
print(extracted_string)
output
waqas
You can convert the string into a dictionary with json.loads()
import json
mystring = b'{"personId":"65a83de6-b512-4410-81d2-ada57f18112a","persistedFaceIds":["792b31df-403f-4378-911b-8c06c06be8fa"],"name":"waqas"}'
mydict = json.loads(mystring)
print(mydict["name"])
# output 'waqas'
First you need to convert the string into a proper JSON Format by removing b from the string using substring in python suppose you have a variable x :
import json
x = x[1:];
dict = json.loads(x) //convert JSON string into dictionary
print(dict["name"])
I having some json format like
json= 5843080158430803{"name":"NAME", "age":"56",}
So, how i get {"name":"NAME", "age":"56",} Using regex/split (which one is bets method for it) in Python.
Thanks in Advance...
Split the first occurance of { into an array, and get the second element in the array.
We also have to add the { again because its removed by the split function
json = '5843080158430803{"name":"NAME", "age":"56",}'
json = '{' + json.split('{', 1)[1]
print(json)
Result: {"name":"NAME", "age":"56",}
perhaps you could split at at the first { and then replace the part prior to it.
I am assuming the json you have above is actually a string. Then you could do:
json_prefix = json.split("{")
json = json.replace(json_prefix, "")
How does one remove a header from a long string of text?
I have a program that displays a FASTA file as
...TCGATCATCGATCG>IonTorrenttrimmedcontig1$CCGTAGGTGAACCTGCGGAAG...
The string is large and contains multiple headers like this
So the headers that need to be trimmed start with a > and end with a $
There's multiple headers, ranging from IonTorrenttrimmedcontig1 to IonTorrenttrimmedcontig25
How can I cut on the > and the $, remove everything inbetween, and seperate the code before and after into seperate list elements?
The file is read from a standard FASTA file, so I´d be very happy to hear possible solutions on the input step as well.
As it is part of fasta file, so you are going to slice it like this:
>>> import re
>>> a = "TCGATCATCGATCG>IonTorrenttrimmedcontig1$CCGTAGGTGAACCTGCGGAAG"
>>> re.split(">[^$]*\$", a)
['TCGATCATCGATCG', 'CCGTAGGTGAACCTGCGGAAG']
Also, some people are answering with slicing with '>ion1'. That's totally wrong!
I believe your problem is solved! I am also editing a tag with bioinformatics for this question!
I would use the re module for that:
>>> s = "blablabla>ion1$foobar>ion2$etc>ion3$..."
>>> import re
>>> re.split(">[^$]*\$",s)
['blablabla', 'foobar', 'etc', '...']
And if you have 1 string on each line:
>>> with open("foo.txt", "r") as f:
... for line in f:
... re.split(">[^$]*\$",line[:-1])
...
['blablabla', 'foobar', 'etc', '...']
['fofofofofo', 'barbarbar', 'blablabla']
If you are reading over every line there a few ways to do this. You could use partition (partition returns a list containing 3 elements: [the text before the specified string, the specified string, and the text after]):
for line in file:
stripped_header = line.partition(">")[2].partition("$")[0]
You could use split:
for line in file:
stripped_header = line.spilt(">")[1].split("$")[0]
You could loop over all the elements in the string and only append after you pass ">" but before "$" (however this will not be nearly as efficient):
for line in file:
bool = False
stripped_header = ""
for char in line:
if char == ">":
bool = True
elif bool:
if char != "$":
stripped_header += char
else:
bool = False
Or alternatively use a regular expression, but it seems like my peers have already beat me to it!