[Tutor] Newline issues
Alex Kleider
akleider at sonic.net
Mon May 11 15:50:33 EDT 2020
On 2020-05-11 11:36, boB Stepp wrote:
> My end goal: Get my Google contacts into Mutt.
Are you trying to create a "~/.muttalias" file?
If so, I can send you some code that accomplishes this (using a gmail
exported contacts.csv file as input.)
Let me know.
Alex
>
> My process so far: Export contacts from Google Contacts as a vCard
> file.
> Use VObject (http://eventable.github.io/vobject/) to parse the vCard
> file. But
> the file that exports from Google-land has some issues that causes
> VCard to throw parsing exceptions. I have identified in contacts.vcf
> (the vCard file) the problems that cause the parsing exceptions. (1)
> All
> address lines have a duplicate entry tacked onto the end with embedded
> '\n's. (2) In the notes entry there are embedded '\n's.
>
> So I first was going to address the embedded '\n's by using a
> s.replace('\n', ' ') approach. This failed. So I wrote the following
> test
> program to try to understand what was happening:
>
> with open("test.txt") as f:
> for line in f:
> print(line)
> print(line.replace("\n", ""))
> print(line.replace(r"\n", ""))
> print(line.replace("\\", ""))
>
> test.txt (an actual excerpt from a fake Google contact export):
>
> NOTE:This is a test contact to see all fields.\n2nd line\: Did not
> fill in
> \"Chat\" field.\n3rd line\: Did not fill in \"Internet call\"
> field.\n4t
> h line\: I will keep typing until the editor flows this text to the
> follo
> wing line. It will now happen.\n1st custom label: My first custom
> field\n
> 2nd custom label: My second custom field\n3rd custom label: My third
> custo
> m field
>
> Running the program on this file produced the following output:
>
> NOTE:This is a test contact to see all fields.\n2nd line\: Did not
> fill in
>
> NOTE:This is a test contact to see all fields.\n2nd line\: Did not
> fill in
> NOTE:This is a test contact to see all fields.2nd line\: Did not fill
> in
>
> NOTE:This is a test contact to see all fields.n2nd line: Did not fill
> in
> [-- snipped remaining output --]
>
> The first line appears as is.
>
> The second had no effect on replacing the apparent '\n' characters.
>
> The third line shows that using raw strings *did* remove the apparent
> '\n'
> characters.
>
> Finally, the fourth line (removing the forward slashes) did so leaving
> all
> associated 'n's.
>
> My conclusion is that in the file the apparent newline characters are
> actually *two* characters, '\' and 'n'.
>
> Now we get to my point of confusion. When I copy and paste the
> text.txt
> contents into a triple-quoted string variable in the interpreter I get
> results I am not sure I understand:
>
> 3.7.5: s = """
> ... NOTE:This is a test contact to see all fields.\n2nd line\:
> Did not fill in
> ... \"Chat\" field.\n3rd line\: Did not fill in \"Internet
> call\" field.\n4t
> ... h line\: I will keep typing until the editor flows this text
> to the follo
> ... wing line. It will now happen.\n1st custom label: My first
> custom field\n
> ... 2nd custom label: My second custom field\n3rd custom label:
> My third custo
> ... m field
> ... """
> 3.7.5: s
> '\nNOTE:This is a test contact to see all fields.\n2nd line\\: Did
> not fill in\n "Chat" field.\n3rd line\\: Did not fill in "Internet
> call" field.\n4t\n h line\\: I will keep typing until the editor
> flows this text to the follo\n wing line. It will now happen.\n1st
> custom label: My first custom field\n\n 2nd custom label: My second
> custom field\n3rd custom label: My third custo\n m field\n'
> 3.7.5: print(s)
>
> NOTE:This is a test contact to see all fields.
> 2nd line\: Did not fill in
> "Chat" field.
> 3rd line\: Did not fill in "Internet call" field.
> 4t
> h line\: I will keep typing until the editor flows this text to the
> follo
> wing line. It will now happen.
> 1st custom label: My first custom field
>
> 2nd custom label: My second custom field
> 3rd custom label: My third custo
> m field
>
> I am not certain how just entering "s" in the interpreter will display
> in
> your MUAs, but in my display it as if the natural line breaks have
> vanished with the exception of the manual one I entered immediately
> after
> the first triple-quote and what I entered just before the final
> triple-quote.
>
> Finally, the print(s) apparently treats those '\' + 'n' characters as a
> genuine newline. Would someone please clarify the interpreter behavior
> for
> me and the difference from what I observed with the actual file?
More information about the Tutor
mailing list