0

I coudn't find a solution and need help. I read a very hugh vCard text file with readline() on python3. Now I have the problem, that some long text lines goes over multiple line like:

item1.ADR;type=HOME;type=pref:;;27-28\, Yakyu-cho 5-chome\nTokyo;Higashimat
 suyama-shi;Saitama;355-8603;Japan

I search the content with:

if re.search (r'.ADR;', elm):
    adr_list.append(elm)

may list looks:

['', '', '27-28\\, Yakyu-cho 5-chome\\nTokyo', 'Higashimat\n']

with line.strip():

 ['', '', '27-28\\, Yakyu-cho 5-chome\\nTokyo', 'Higashimat']

The desired output should look like this:

['', '', '27-28', 'Yakyu-cho 5-chome','Tokyo', 'Higashimatsuyama-shi','Saitama','355-8603','Japan']

I used also rstrip(), but I can't get the right solution.

Is there an easy way with readline() to detect the linebreak and concatenate the next related line/s?

Hermann12
  • 1,709
  • 2
  • 5
  • 14
  • Does this answer your question? [Concatenate strings read from file with python?](https://stackoverflow.com/questions/3788426/concatenate-strings-read-from-file-with-python) – Umair Mubeen Jan 16 '21 at 14:52
  • your desired output? – Umair Mubeen Jan 16 '21 at 14:58
  • @UmairMubeen, thanks for your hint, but in my case I have to use readline() not readlines(). – Hermann12 Jan 16 '21 at 14:59
  • @Hermann12: Can you make sure a complete 'data line' is just spanning over a single line without having line breaks inside? – albert Jan 16 '21 at 15:03
  • @albert, Thanks. Good idea, I think. But I don't know how, I am new in python. The file .txt have thousend of lines. some of them (ADR) looks like my first code block example. The very most have only one line. I used line.strip(), but doesn't solve my problem. – Hermann12 Jan 16 '21 at 15:11
  • Did you think about using an already available parser to read your vCard file? https://stackoverflow.com/questions/35825919/vcard-parser-with-python – albert Jan 16 '21 at 15:23
  • Solved. Don't allowed to post my solution, sorry. – Hermann12 Jan 16 '21 at 16:43

0 Answers0