2

I want to remove the last comma in a text file in python. The file looks like this :

{"time":"2019-02-12T14:41:25.2424552+01:00","ip":"::1","browser":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36","user":"28969523B","profile":"SUMUM_D?99?2017?2","controller":"Alteraciones_Primitivas_dmz_","service":"GetAlteracion","delegacion":null,"municipio":null,"method":"GET","parameters":{"id_alteracion":"11_31_85531"},"response":null,"status":200},  
{"time":"2019-02-12T14:41:25.5525828+01:00","ip":"::1","browser":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36","user":"28969523B","profile":"SUMUM_D?99?2017?2","controller":"Expedientes_dmz_","service":"GetObligatoriedadDocumental","delegacion":null,"municipio":null,"method":"GET","parameters":{"id_alteracion":"11_31_85531","tipo_alteracion":"ALT_1","id_expediente":""},"response":null,"status":400},  
{"time":"2019-02-12T14:41:25.9169746+01:00","ip":"::1","browser":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36","user":"28969523B","profile":"SUMUM_D?99?2017?2","controller":"Alteraciones_Primitivas_dmz_","service":"Ubicar","delegacion":null,"municipio":null,"method":"GET","parameters":{"id_alteracion":"11_31_85531","epsg":"EPSG:4326"},"response":null,"status":200},
{"time":"2019-02-12T14:41:29.8222248+01:00","ip":"::1","browser":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36","user":"28969523B","profile":"SUMUM_D?99?2017?2","controller":"Alteraciones_Primitivas_dmz_","service":"GetAlteracion","delegacion":null,"municipio":null,"method":"GET","parameters":{"id_alteracion":"11_31_17819"},"response":null,"status":200},  
{"time":"2019-02-12T14:41:29.9708861+01:00","ip":"::1","browser":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36","user":"28969523B","profile":"SUMUM_D?99?2017?2","controller":"Expedientes_dmz_","service":"GetObligatoriedadDocumental","delegacion":null,"municipio":null,"method":"GET","parameters":{"id_alteracion":"11_31_17819","tipo_alteracion":"ALT_1","id_expediente":""},"response":null,"status":400} ,  
{"time":"2019-02-12T14:41:30.3077709+01:00","ip":"::1","browser":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36","user":"28969523B","profile":"SUMUM_D?99?2017?2","controller":"Alteraciones_Primitivas_dmz_","service":"Ubicar","delegacion":null,"municipio":null,"method":"GET","parameters":{"id_alteracion":"11_31_17819","epsg":"EPSG:4326"},"response":null,"status":200},  
{"time":"2019-02-12T14:41:38.7145697+01:00","ip":"::1","browser":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36","user":"28969523B","profile":"SUMUM_D?99?2017?2","controller":"Documentos_dmz_","service":"obtenerDocumento","delegacion":null,"municipio":null,"method":"GET","parameters":{"id_alteracion":"11_31_17819","documento":"E000226847","control":11},"response":null,"status":200},  

And I want to remove the comma in the last line. After looking a plenty of examples, I tried this code (and a lot of similar options):

with open('data.txt', 'w') as f:    
    f.seek(f.tell() - 1, os.SEEK_SET)
    f.truncate()

But this doesn't work. Any help?

EDIT

There are no spaces between the comma and '}'. The program we have in my office (I don't have access to it) gives an error if the file ends with a comma. What I need is to remove only the last comma in the last line of the file, not in every line, and return the file in this way:

{"time":"2019-02-12T14:41:25.2424552+01:00","ip":"::1","browser":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36","user":"28969523B","profile":"SUMUM_D?99?2017?2","controller":"Alteraciones_Primitivas_dmz_","service":"GetAlteracion","delegacion":null,"municipio":null,"method":"GET","parameters":{"id_alteracion":"11_31_85531"},"response":null,"status":200},  
{"time":"2019-02-12T14:41:25.5525828+01:00","ip":"::1","browser":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36","user":"28969523B","profile":"SUMUM_D?99?2017?2","controller":"Expedientes_dmz_","service":"GetObligatoriedadDocumental","delegacion":null,"municipio":null,"method":"GET","parameters":{"id_alteracion":"11_31_85531","tipo_alteracion":"ALT_1","id_expediente":""},"response":null,"status":400},  
{"time":"2019-02-12T14:41:25.9169746+01:00","ip":"::1","browser":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36","user":"28969523B","profile":"SUMUM_D?99?2017?2","controller":"Alteraciones_Primitivas_dmz_","service":"Ubicar","delegacion":null,"municipio":null,"method":"GET","parameters":{"id_alteracion":"11_31_85531","epsg":"EPSG:4326"},"response":null,"status":200},
{"time":"2019-02-12T14:41:29.8222248+01:00","ip":"::1","browser":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36","user":"28969523B","profile":"SUMUM_D?99?2017?2","controller":"Alteraciones_Primitivas_dmz_","service":"GetAlteracion","delegacion":null,"municipio":null,"method":"GET","parameters":{"id_alteracion":"11_31_17819"},"response":null,"status":200},  
{"time":"2019-02-12T14:41:29.9708861+01:00","ip":"::1","browser":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36","user":"28969523B","profile":"SUMUM_D?99?2017?2","controller":"Expedientes_dmz_","service":"GetObligatoriedadDocumental","delegacion":null,"municipio":null,"method":"GET","parameters":{"id_alteracion":"11_31_17819","tipo_alteracion":"ALT_1","id_expediente":""},"response":null,"status":400},  
{"time":"2019-02-12T14:41:30.3077709+01:00","ip":"::1","browser":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36","user":"28969523B","profile":"SUMUM_D?99?2017?2","controller":"Alteraciones_Primitivas_dmz_","service":"Ubicar","delegacion":null,"municipio":null,"method":"GET","parameters":{"id_alteracion":"11_31_17819","epsg":"EPSG:4326"},"response":null,"status":200},  
{"time":"2019-02-12T14:41:38.7145697+01:00","ip":"::1","browser":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36","user":"28969523B","profile":"SUMUM_D?99?2017?2","controller":"Documentos_dmz_","service":"obtenerDocumento","delegacion":null,"municipio":null,"method":"GET","parameters":{"id_alteracion":"11_31_17819","documento":"E000226847","control":11},"response":null,"status":200}

EDIT 2 Finally, @holdenweb solution work for me! :

with open('data.txt', 'ab'):
    f.seek(-1, 2)
    f.truncate()
rpanai
  • 12,515
  • 2
  • 42
  • 64
Aw3same
  • 930
  • 4
  • 13
  • 38
  • 1
    Possible duplicate of [Python - Remove very last character in file](https://stackoverflow.com/questions/18857352/python-remove-very-last-character-in-file) – liamhawkins Feb 12 '19 at 17:00
  • Are you sure the comma is the last character in the file and you don't have anything hidden (e.g. an empty space or carriage return or line feed symbol (\r or \n))? – tnknepp Feb 12 '19 at 17:01
  • @liamhawkins the potential dupe you mention makes the assumption that one character = one byte. – holdenweb Feb 12 '19 at 17:06

5 Answers5

3

Use this:

res = your_string.rstrip(',')

For documentation

Hope it helps Thanks

Avinash Singh
  • 4,970
  • 8
  • 20
  • 35
  • This is a great solution, but only works if the comma is the last character, which I doubt is the case here. – tnknepp Feb 12 '19 at 17:04
3

Your code didn't work because opening a file in write mode immediately truncates it to zero length. You need append mode, which will use the content of any existing file.

Ignoring the assumption that one byte equals one character (not always the case once Unicode enters the picture), and assuming there is no newline at the end of the file and that you want to retain the space, this should work:

with open('data.txt', 'ab'):
    f.seek(-1, 2)
    f.truncate()
holdenweb
  • 33,305
  • 7
  • 57
  • 77
2

I noticed that , is not the last symbol in your data, that's why your solution as well as rstrip(',') won't work. Use replace instead:

with open('data.txt', 'r') as f:
    data = f.read().replace('} ,', '}')

with open('data.txt', 'w') as f:
    f.write(data)

This script removes last comma on each line (since '} ,' appears only at the end of each line) and you'll get following on ouput:

{"time":"2019-02-12T14:41:25.2424552+01:00","ip":"::1","browser":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36","user":"28969523B","profile":"SUMUM_D?99?2017?2","controller":"Alteraciones_Primitivas_dmz_","service":"GetAlteracion","delegacion":null,"municipio":null,"method":"GET","parameters":{"id_alteracion":"11_31_85531"},"response":null,"status":200}
{"time":"2019-02-12T14:41:25.5525828+01:00","ip":"::1","browser":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36","user":"28969523B","profile":"SUMUM_D?99?2017?2","controller":"Expedientes_dmz_","service":"GetObligatoriedadDocumental","delegacion":null,"municipio":null,"method":"GET","parameters":{"id_alteracion":"11_31_85531","tipo_alteracion":"ALT_1","id_expediente":""},"response":null,"status":400}
{"time":"2019-02-12T14:41:25.9169746+01:00","ip":"::1","browser":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36","user":"28969523B","profile":"SUMUM_D?99?2017?2","controller":"Alteraciones_Primitivas_dmz_","service":"Ubicar","delegacion":null,"municipio":null,"method":"GET","parameters":{"id_alteracion":"11_31_85531","epsg":"EPSG:4326"},"response":null,"status":200}
{"time":"2019-02-12T14:41:29.8222248+01:00","ip":"::1","browser":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36","user":"28969523B","profile":"SUMUM_D?99?2017?2","controller":"Alteraciones_Primitivas_dmz_","service":"GetAlteracion","delegacion":null,"municipio":null,"method":"GET","parameters":{"id_alteracion":"11_31_17819"},"response":null,"status":200}
{"time":"2019-02-12T14:41:29.9708861+01:00","ip":"::1","browser":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36","user":"28969523B","profile":"SUMUM_D?99?2017?2","controller":"Expedientes_dmz_","service":"GetObligatoriedadDocumental","delegacion":null,"municipio":null,"method":"GET","parameters":{"id_alteracion":"11_31_17819","tipo_alteracion":"ALT_1","id_expediente":""},"response":null,"status":400}
{"time":"2019-02-12T14:41:30.3077709+01:00","ip":"::1","browser":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36","user":"28969523B","profile":"SUMUM_D?99?2017?2","controller":"Alteraciones_Primitivas_dmz_","service":"Ubicar","delegacion":null,"municipio":null,"method":"GET","parameters":{"id_alteracion":"11_31_17819","epsg":"EPSG:4326"},"response":null,"status":200}
{"time":"2019-02-12T14:41:38.7145697+01:00","ip":"::1","browser":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36","user":"28969523B","profile":"SUMUM_D?99?2017?2","controller":"Documentos_dmz_","service":"obtenerDocumento","delegacion":null,"municipio":null,"method":"GET","parameters":{"id_alteracion":"11_31_17819","documento":"E000226847","control":11},"response":null,"status":200}
Alderven
  • 7,569
  • 5
  • 26
  • 38
2

How about this.

rf = open('data.txt').read()
with open('data.txt', 'w') as wf:
    wf.write(rf[:-3])
  • The question is clearly trying use file truncation, presumably for "efficiency". This method creates a new file rather than shortening the existing one. – holdenweb Feb 12 '19 at 17:08
1

This solution locates the index of the last } in the last line and replaces the string up to the index.

Optionally rename the "new" file afterwards.

with open("datain.txt", 'r') as f_in, open('dataout.txt', 'w') as f_out:
    last = None
    for l in f_in.readlines():
        if last:
            f_out.write(last)
        last = l
    i = last.rfind("}")
    f_out.write(last[:i+1])

You could also do something like this, however I don't know how it handles encodings and the loop could be limited more so you don't end up truncating the entire file.

import os
with open("datain.txt", "r+") as f:
    f.seek(0, os.SEEK_END)
    pos = f.tell()

    while pos > 0:
        f.seek(pos)
        c = f.read(1).encode('utf-8')
        if c == b'}':
            break
        pos = f.tell() - 2
    f.truncate(pos)
Jeppe
  • 1,830
  • 3
  • 24
  • 33