2

I have a file called path_text.txt its contents are the 2 strings separated by newline:

/gp/oi/eu/gatk/inputs/NA12878_24RG_med.hg38.bam 
/gp/oi/eu/gatk/inputs/NA12878_24RG_small.hg38.bam

I would like to have a json array object like this:

["/gp/oi/eu/gatk/inputs/NA12878_24RG_med.hg38.bam","/gp/oi/eu/gatk/inputs/NA12878_24RG_small.hg38.bam"]

I have tried something like this:

with open('path_text.txt','w',encoding='utf-8') as myfile:
    myfile.write(','.join('\n'))

But it does not work

3
  • 3
    You say you want to read the file to JSON, but your code is writing to the file. Which one are you actually trying to do? If you're trying to read, why not just file.readlines()? Commented Nov 12, 2020 at 9:15
  • 1
    read yes. I'm sorry, I was trying to write comma (,) and double quotes (") into the existing file. thats why i used write Commented Nov 12, 2020 at 9:21
  • 1
    Does this answer your question? How to read a file line-by-line into a list? followed by How do I write JSON data to a file? Commented Nov 12, 2020 at 9:28

2 Answers 2

4

I don't see where you're actually reading from the file in the first place. You have to actually read your path_text.txt before you can format it correctly right?

with open('path_text.txt','r',encoding='utf-8') as myfile:
    content = myfiel.read().splitlines()

Which will give you ['/gp/oi/eu/gatk/inputs/NA12878_24RG_med.hg38.bam', '/gp/oi/eu/gatk/inputs/NA12878_24RG_small.hg38.bam'] in content.

Now if you want to write this data to a file in the format ["/gp/oi/eu/gatk/inputs/NA12878_24RG_med.hg38.bam", "/gp/oi/eu/gatk/inputs/NA12878_24RG_small.hg38.bam"]-

import json

with open('path_json.json', 'w') as f:
    json.dump(content, f)

Now the path_json.json file looks like-

["/gp/oi/eu/gatk/inputs/NA12878_24RG_med.hg38.bam", "/gp/oi/eu/gatk/inputs/NA12878_24RG_small.hg38.bam"]

which is valid json in case you want to load a json from a file

Sign up to request clarification or add additional context in comments.

2 Comments

@Tomerikoo true, I have no idea how I managed to forget using json.dump in my answer at first while simultaneously mentioning it in a comment
@Chase yeah I noticed that in the moment ^_^ All good... now it looks fine :)
3

see below

with open('path_text.txt') as f:
    data = [l.strip() for l in f.readlines()]

9 Comments

list comprehension is good but note that single quotes are the result, JSON needs double quotes
We have list of strings here. The quote type in not important.
@alex what? single quotes are just how python represent strings, that does not mean anything at all. That will still be valid json when you use json.dumps
@Chase I think the OP is confused with single-quotes when reading a file that can't be read as JSON
@alex then why don't you use the built-in json module?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.