开发者

How can you remove the new line characters in python?

开发者 https://www.devze.com 2023-01-27 18:21 出处:网络
I am wr开发者_运维百科iting a vcf parser and I have the file open but now I need to parse their first name. The file shows \"FN:John Smith;;;\\n\\r\" I want to take out the \\n and \\r. Can anybody he

I am wr开发者_运维百科iting a vcf parser and I have the file open but now I need to parse their first name. The file shows "FN:John Smith;;;\n\r" I want to take out the \n and \r. Can anybody help me?


Use the rstrip function:

s = s.rstrip()

This will remove all whitespace from the end of your string.


If as you say the file shows "FN:John Smith;;;\n\r", then you have a problem -- that \r is totally unexpected.

What operating system are you using, what version of Python, and exactly how did you determine that the file shows that?

Here is the usual idiom for reading a file that has lines terminated by the terminator usually used by the OS you are using, and has fields separated by ; characters:

f = open('myfile.txt', 'r')
for line in f:
    # standard OS terminator is converted to `\n` 
    line = line.rstrip('\n') # remove trailing newline
    fields = line.split(';')
    # fields[0] should refer to "FN:John Smith" in your example
    for field_index, field in enumerate(fields):
        if not field:
            continue # empty field
        tag, value = field.split(':')
        print "Field %d: tag %r, value %r" % (field_index, tag, value)

You may not have read this Wikipedia article ... I note that "FN" means "Formatted Name", not "First Name", and there's an "N" tag that would be easier to parse:

N:Gump;Forrest
FN:Forrest Gump

I also note that a line like FN:John Smith;;; doesn't appear in the article.

You may be able to use existing code; see this StackOverflow question.

0

精彩评论

暂无评论...
验证码 换一张
取 消