#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,988
    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,988
    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,206
    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,988
    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,988
    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'
  22. #12
  23. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,206
    Rep Power
    2012
    If you only want distinct values, you can place the result in a set before join:
    python Code:
    print(''.join(set(result)).encode('ascii'))
  24. #13
  25. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,988
    Rep Power
    510
    Code:
    '''
    desired output based on post #9
    \n \r\n
    '''
    
    LF_count = 0
    CR_count = 0
    
    result = []
    
    with open('data1.txt','rb') as inf:
        for c in inf.read():
            if (c == ord('\n')) and (LF_count <= CR_count):
                result.append(r'\n')
                if LF_count < 1:
                    result.append(' ')
                LF_count += 1
            elif c == ord('\r'):
                CR_count += 1
                result.append(r'\r')
    
    print(''.join(result))
    The input file is created with this program, which is my best guess as to what was intended by the given input.
    Code:
    NB. www.jsoftware.com  the j executable language
    (<'data1.txt')1!:2~a.{~49 49 44 65 108 108 97 110  44  56 47  50  54  47  50  48  49  49  10  49  50 44 66 111 98 44 57 47  50  54 47  50  48  49 50  13  10  49  51  44  99 104 114 105 115 44 57  47 50 55 47 50  48  49 53  10  49  52 44 100  97 110 105 101 108  44  49  49  47 56 47  50 48 49 54 10  49  53 44  69 100 119 97 114 100  44  49  50  47  53  46  50  48 49 55  10
    Last edited by b49P23TIvg; April 21st, 2019 at 09:11 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo