#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2019
    Posts
    6
    Rep Power
    0

    determining special charecters


    I have a data file named data1.txt with content:
    11,Allan,8/26/2011
    12,Bob,9/26/2012
    13,Cyril,10/25/2014

    But I cannot able to extract the line termination character using this code:

    Code:
    import os
    import collections
    import sys
    import codecs
    import csv
    
    
    inFile=data1.txt
    field = open(inFile,'r').readlines()
    for line in csv_obj:
        print(repr(line))
    it is giving out put as:

    '11,Allan,8/26/2011\n'
    '12,Bob,9/26/2012\n'
    '13,Cyril,10/25/2014'


    I need the out as \n.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2019
    Posts
    6
    Rep Power
    0
    code is:

    import os
    import collections
    import sys
    import codecs
    import csv


    Code:
    inFile=data1.txt
    field = open(inFile,'r').readlines()
    for line in fieldj:
        print(repr(line))
  4. #3
  5. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,980
    Rep Power
    510
    Code:
    inFile = 'data1.txt'
    
    field = []
    
    with open(inFile,'rt') as inf:
        for line in inf:
            if line[-1] == '\n':
                line = line[:-1]
            field.append(line)
    
    print(field)
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2019
    Posts
    6
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Code:
    inFile = 'data1.txt'
    
    field = []
    
    with open(inFile,'rt') as inf:
        for line in inf:
            if line[-1] == '\n':
                line = line[:-1]
            field.append(line)
    
    print(field)

    Using the code I am able to get the output as:

    ['11,Allan,8/26/2011\n', '12,Bob,9/26/2012','11,Allan,8/26/2011']


    In my case the out put will \n. if the line terminator \n\r then out will \n\r.
  8. #5
  9. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,980
    Rep Power
    510
    No big surprise here since I created the input file in unix. Try this pathetic bit:
    Code:
    inFile = 'data1.txt'
    
    field = []
    
    with open(inFile,'rt') as inf:
        for line in inf:
            while line[-1] in '\n\r':
                line = line[:-1]
            field.append(line)
    
    print(field)
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2019
    Posts
    6
    Rep Power
    0
    Originally Posted by b49P23TIvg
    No big surprise here since I created the input file in unix. Try this pathetic bit:
    Code:
    inFile = 'data1.txt'
    
    field = []
    
    with open(inFile,'rt') as inf:
        for line in inf:
            while line[-1] in '\n\r':
                line = line[:-1]
            field.append(line)
    
    print(field)

    No I am not getting the outout as \n instead I am getting it as :

    [ '11,Allan,8/26/2011', '12,Bob,9/26/2012']
  12. #7
  13. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,201
    Rep Power
    2012
    Can you post how exactly you want the complete result/output to be like?
  14. #8
  15. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,980
    Rep Power
    510
    um...yeah..good question..
    [code]Code tags[/code] are essential for python code and Makefiles!
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2019
    Posts
    6
    Rep Power
    0

    line terminators


    Originally Posted by MrFujin
    Can you post how exactly you want the complete result/output to be like?
    if the input file is
    Code:
    11,Allan,8/26/2011\n
    12,Bob,9/26/2012\r\n
    13,chris,9/27/2015\n
    14,daniel,11/8/2016\n
    15,Edward,12/5.2017

    the out put will be
    Code:
    \n \r\n
    . it will print distinct line terminators
  18. #10
  19. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,980
    Rep Power
    510
    Code:
    #! /usr/bin/python3
    
    ''' this is sort of close '''
    
    result = []
    
    with open('p.py','rt') as inf:
        result = [c for c in inf.read() if c in '\n\r ']
    
    print(''.join(result).encode('ascii'))
    [code]Code tags[/code] are essential for python code and Makefiles!
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2019
    Posts
    6
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Code:
    #! /usr/bin/python3
    
    ''' this is sort of close '''
    
    result = []
    
    with open('p.py','rt') as inf:
        result = [c for c in inf.read() if c in '\n\r ']
    
    print(''.join(result).encode('ascii'))
    The solution was quite close but slight away from my output..

    Code:
    11,Allan,8/26/2011
    12,Bob,9/26/2012
    13,Charles,10/23/2013
    14,Edward,3/12/2014
    I should get:
    Correct:
    Code:
     \n
    But getting:
    Code:
    b'\n\n\n'

IMN logo majestic logo threadwatch logo seochat tools logo