0

I have a code, which takes a string from input_file_1 and two numbers to slice the string from input_file_2, the output strings are written in output_file and divided by space:

def task_3(input_file_1, input_file_2, output_file):
    with open(input_file_1) as i_f_1, open(input_file_2) as i_f_2, open(output_file, 'w') as o_f:
        result = []
        for line_1, line_2 in zip(i_f_1.readlines(), i_f_2.readlines()):
            start, finish = line_2.split()
            result.append(line_1[int(start):int(finish)+1])
        o_f.write(' '.join(repr(i) for i in result))

task_3('test_import_file_2_1.txt', 'test_import_file_2_2.txt', 'output_file.txt')

The problem is that output looks like this:

'tPGRIXNp' 'JEflNBYRbuLkOiZqQHYNNTWPUIPNibcIpSL' 'yDvBXxyOUwjGAemSbsuHupMmGIWpTwv'...

Whereas it must look like this:

tPGRIXNp JEflNBYRbuLkOiZqQHYNNTWPUIPNibcIpSL yDvBXxyOUwjGAemSbsuHupMmGIWpTwv...

As you can see strings are attached to single quotes, which I can't get rid off.

  • 1
    Does this solves your issue https://stackoverflow.com/questions/12453580/how-to-concatenate-items-in-a-list-to-a-single-string BTW I think you don't need to use `repr` as your list only has string, and in any case you should be using `str` instead of `repr`. – Francisco Puga Mar 20 '22 at 09:30
  • No, I already used `join` method. I need to remove quotes somehow. UPD. I have removed `repr` without using `str` and everything is alright. No quotes. Thank you. Could you please post this as an answer? –  Mar 20 '22 at 09:33
  • Does this answer your question? [python write string to a file without quotes](https://stackoverflow.com/questions/40857300/python-write-string-to-a-file-without-quotes) – kcsquared Mar 20 '22 at 09:39
  • 1
    On a side note, [if you're curious](https://stackoverflow.com/a/30315372/16757174) about why `repr()` adds quotes: from the [official docs](https://docs.python.org/3/library/functions.html#repr) for `repr()`: "this function makes an attempt to return a string that would yield an object with the same value when passed to eval()" – kcsquared Mar 20 '22 at 09:45

4 Answers4

0

You can try removing function "repr"

o_f.write(' '.join(i for i in result))

Also you can provide an example of your input files to reproduce the problem on my computer.

  • Thank you, that was a problem. Any thoughts why `repr` caused the issue? –  Mar 20 '22 at 09:37
  • 1
    The repr() function returns a printable representation of the given object. If you give to repr an string as parameter `repr()` adds the `' '` You can try this: `print(repr('hello'))` `print('hello')` – Masizo Robles Mar 20 '22 at 09:43
0

Just remove repr and you'll get what you want. The reason: repr("string") = "'string'". So it adds a pair of single quotes to every line of your output.

yzhang
  • 122
  • 3
0

Usually the builtin that you must use to convert a value to string in Python is str, not repr.

str and repr call the methods __str__ and __repr__ of the object. You can check in this answer how they work and that are the use cases.

Francisco Puga
  • 23,869
  • 5
  • 48
  • 64
0

If you want to just remove "'", you can just use

String_name.replace("'","")
Faraaz Kurawle
  • 1,085
  • 6
  • 24