-2

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

  • What are you planning to do with the content once you remove the last `,` – Devesh Kumar Singh Jun 19 '19 at 06:25
  • i am running a loop and i dont need the last , need to remove that for further parsing –  Jun 19 '19 at 06:26
  • Can you add that code as well where you are parsing these strings – Devesh Kumar Singh Jun 19 '19 at 06:27
  • What format is the txt file, custom? Are you parsing JSON with regex? – Andrej Kesely Jun 19 '19 at 06:28
  • building a random JSON data in a particular format in bulk using faker lib. so needed to remove the last , –  Jun 19 '19 at 06:31
  • 7
    I think it's better if you fix the problem at source, since what you have is not a valid json string, so fix your source to generate correct json strings – Devesh Kumar Singh Jun 19 '19 at 06:33
  • 1
    Just a heads up - it will also fail because the property names are not quoted. – Adam.Er8 Jun 19 '19 at 06:33
  • I think this problem should be solved at the source. How are you creating that file? If you properly do it, no need to go through all this. – Vishnudev Krishnadas Jun 19 '19 at 06:33
  • If you have any control of the code that produces the files - you should fix it to produce valid JSONs – Adam.Er8 Jun 19 '19 at 06:34
  • just changing it to `{ "hello" : 1,"two":"three","four":"five","six":"seven" } ` will solve your problem – Devesh Kumar Singh Jun 19 '19 at 06:35
  • if i try parsing file/removing the last delimiter (,) with regex it would a cause prblm ? need suggestions. –  Jun 19 '19 at 06:35
  • 1
    Your problem is in generating the JSON. Put your data in correct structure (dict or list) and use `json.dump()` – Andrej Kesely Jun 19 '19 at 06:37
  • 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 –  Jun 19 '19 at 06:41
  • Well whatever regex you can come up with will be complex, and can break if not thoroughly tested – Devesh Kumar Singh Jun 19 '19 at 06:43
  • oh.. ok vl give it a try. –  Jun 19 '19 at 06:44
  • every time i generate it . the delimited would be second from last in the json. may be if ican find a way to remove the last second character in the file. It might work . any suggestions ? @DeveshKumarSingh –  Jun 19 '19 at 06:49
  • You can use an alternative json parser such as https://github.com/dmeranda/demjson/ that can parse invalid `json` (check out the `allow_trailing_comma` option) – Selcuk Jun 19 '19 at 06:51
  • That would be another question, where you show us the code which generates these json – Devesh Kumar Singh Jun 19 '19 at 06:51
  • Possible duplicate of [How to decode an invalid json string in python](/questions/12511159/how-to-decode-an-invalid-json-string-in-python) – tripleee Jun 19 '19 at 07:26

4 Answers4

1

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
}
Albo
  • 1,584
  • 9
  • 27
  • So you are reversing the string twice, just to replace the last comma? – Devesh Kumar Singh Jun 19 '19 at 07:02
  • In fact, yes, but this just changes the representation of the string – Albo Jun 19 '19 at 07:04
  • But that's extra computation, and really inefficient for longer strings Also this gives you a string, but you cannot use it to create a dictionary – Devesh Kumar Singh Jun 19 '19 at 07:05
  • This way is the fastest way possible to reverse strings because you just change the representation (see [first](https://stackoverflow.com/a/27843760/8597864) or [second](https://www.journaldev.com/23647/python-reverse-string)) – Albo Jun 19 '19 at 11:45
  • It still is `O(n)` where n is the length of the list, not sure what you mean by fastest here – Devesh Kumar Singh Jun 19 '19 at 11:49
1

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
-2
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.

petezurich
  • 9,280
  • 9
  • 43
  • 57
Akalya Raj
  • 68
  • 1
  • 7
  • 2
    And what if there are commas in the value or the key e.g `{ "tw,o":"three","four":"fi,ve" }` – Devesh Kumar Singh Jun 19 '19 at 06:41
  • 1
    This is horribly inefficient as it reprocesses the whole text file every time it finds a comma. Also it won't work when there are more than one dictionary. – Selcuk Jun 19 '19 at 06:43
  • This only replaces the last comma. Did you run it ? @DeveshKumarSingh . This was just an idea of how it work for the type(text) you had mentioned ,Selcuk. If you want it for more dictionaries.I would suggest you to write a function to loop through all the text. – Akalya Raj Jun 19 '19 at 06:45
  • Also how do you convert your output to a dict later? Because you won't be able to, give it a try – Devesh Kumar Singh Jun 19 '19 at 06:48
  • @AkalyaRaj It regenerates and overwrites `string2` for every comma. – Selcuk Jun 19 '19 at 06:53
  • I have posted another code for efficiency.In any case,the question only implied one string @Selcuk – Akalya Raj Jun 19 '19 at 06:54
  • @DeveshKumarSingh : https://stackoverflow.com/questions/988228/convert-a-string-representation-of-a-dictionary-to-a-dictionary – Akalya Raj Jun 19 '19 at 06:56
  • @AkalyaRaj Have you tried converting it yourself? It won't work. – Selcuk Jun 19 '19 at 07:00
  • Yeah,sorry my bad.Just tried.But i tried another way of splitting and creating Dict.It did work.So perhaps there are other ways to do that. – Akalya Raj Jun 19 '19 at 07:08
  • Your newer answer has issues as well – Devesh Kumar Singh Jun 19 '19 at 11:55
-2
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

Akalya Raj
  • 68
  • 1
  • 7
  • 1
    Perhaps better to update your original answer instead of creating a new one? – Devesh Kumar Singh Jun 19 '19 at 06:57
  • 1
    This will delete the last `key: value` pair for valid dictionaries, for example `string ='{hello : 1,two:three,four:five,six:seven}'` – Selcuk Jun 19 '19 at 06:58
  • I am bit new to posting answers ,let me try to do it from next time .Thanks for the tip – Akalya Raj Jun 19 '19 at 07:09
  • You misunderstood me. Your code deletes the last pair in my example. – Selcuk Jun 19 '19 at 07:11
  • @selcuk,Can i know the example.It still works fine for me :? – Akalya Raj Jun 19 '19 at 07:14
  • [The comment from 25 minutes ago](/questions/56661270/how-to-remove-a-particular-character-from-a-txt-file/56662142#comment99892291_56661688) has a specific example which produces `{hello : 1,two:three,four:five}` where it should produce `{hello : 1,two:three,four:five,six:seven}` i.e. not remove the last key when there isn't a trailing comma. – tripleee Jun 19 '19 at 07:24
  • I am sorry I am not able to understand here.That code doesn't remove the last key,pair.It just extracts the characters before last comma and concatenates a bracket at the end. I think we cab leave at this considering there is already a good answer .Cheers – Akalya Raj Jun 19 '19 at 07:37
  • Try putting `string ='{hello : 1,two:three,four:five,six:seven}'` in your code and you will see that `match.group(1)+"}"` gives you `{hello : 1,two:three,four:five}` – Devesh Kumar Singh Jun 19 '19 at 11:55