0

I'm not a good programmer and I have to be honest in this case I can't find a comfortable solution.

I have a text file derived from the subtitles of a video and I would like to create a more convenient text file. I insert a gif here to show what I would like to do.

I created a macro with word office VB but it doesn't satisfy me.

Would you have any suggestions about it?

Simple Macro Word Office

Sub Macro1()
'
' Macro1 Macro
'
'
    Selection.TypeBackspace
    Selection.TypeBackspace
    Selection.TypeText Text:=" "
End Sub

Simple text in:

a casa il curare la persona a casa
limita anche e razionalizza i costi
della sanità è il paziente e il nostro
familiare che ripeto come dicevo prima è
limitato nel suo corpo e del riciclo in
tutto l'amore di questo mondo quindi
questo è un qualcosa che noi perché per
noi deve diventare come dicevo prima un
obbligo uno stato non deve essere un
qualcosa che dobbiamo fare e siamo
obbligati a farlo chi è già sfortunato
ripeto da chi sicuramente non lo è deve
essere aiutato il tutti quanti modi
necessita per questo o per questa
problematica voi un'idea ce l'avete

Simple text out:

a casa il curare la persona a casa limita anche e razionalizza i costi della sanità è il paziente e il nostro familiare che ripeto come dicevo prima è limitato nel suo corpo e del riciclo in tutto l'amore di questo mondo quindi questo è un qualcosa che noi perché per noi deve diventare come dicevo prima un obbligo uno stato non deve essere un qualcosa che dobbiamo fare e siamo obbligati a farlo chi è già sfortunato ripeto da chi sicuramente non lo è deve essere aiutato il tutti quanti modi necessita per questo o per questa problematica voi un'idea ce l'avete

enter image description here

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Antonio
  • 186
  • 2
  • 12
  • 1
    Can you post sample input/output text here in text format? – Alderven Feb 26 '21 at 07:38
  • Please describe in plain English what this macro does. – martineau Feb 26 '21 at 07:57
  • @Alderven I have inserted a few lines of the input text and how I would like it to be output – Antonio Feb 26 '21 at 18:48
  • @martineau I inserted the word macro, but it is very trivial. I wish it was automatic. – Antonio Feb 26 '21 at 18:51
  • Antonio: Based on your updates, it sounds like you just want to remove the newlines from the file replace them with one long line composed of them all the original ones. Is that correct? – martineau Feb 26 '21 at 19:19
  • @martineau yes, i would like it to be normal multiline text style a word file. to get it it is necessary to eliminate the spaces from two successive lines, as I tried to show in the animated gif – Antonio Feb 26 '21 at 20:19
  • Then I don't understand why you say @tino's answer doesn't do what you want. – martineau Feb 26 '21 at 20:22
  • @martineau because I tried the code and it just gives me an identical copy of the initial file. – Antonio Feb 26 '21 at 20:23

1 Answers1

1

Here's one way to do it:

  1. Read entire file into a list of lines with the newlines between them removed.
  2. Write a new file created by joining all those line into one long one separated by a space character.
with open("separate_lines.txt", "r") as file:
    lines = file.read().splitlines()

with open("merged_lines.txt", "w") as file:
    file.write(' '.join(lines) + '\n')

print('done')

Here's another, slightly more complicated, way that processes the file iteratively a line-at-a-time which eliminates needing to read the whole thing into memory at once:

with open("separate_lines.txt","r") as inp, open("merged_lines.txt","w") as outp:
    outp.write(next(inp).rstrip())  # Read and write first line.
    for line in inp:
        outp.write(' ' + line.rstrip())  # Write following lines prefixed with a space.
    outp.write('\n')  # End output file with a single newline.

print('done')
martineau
  • 119,623
  • 25
  • 170
  • 301
  • your help has been very enlightening. I would like to try to tackle another problem related to the numerical part of the subtitles. – Antonio Feb 27 '21 at 04:52
  • 1
    Sono contento di essere stato in grado di aiutare. – martineau Feb 27 '21 at 07:04
  • I have found several [questions](https://stackoverflow.com/questions/51073045/parsing-transcript-srt-files-into-readable-text) on this subject. – Antonio Feb 28 '21 at 05:33