Separate Rows in reader

Discussion in 'Python' started by Jiewei Huang, Mar 24, 2013.

  1. Jiewei Huang

    Jiewei Huang Guest

    Hi all,

    Currently create a simple text-based database of information about people


    I have a csv file which consist of 3 rows , row 1 2 and 3 is as such:
    Name Address Telephone Birthday
    John Konon Ministry of Moon Walks 4567882 27-Feb
    Stacy Kisha Ministry of Man Power 1234567 17-Jan


    My codes are :
    import csv
    original = file('friends.csv', 'rU')
    reader = csv.reader(original)

    for row in reader:

    print row


    and the output is :
    ['Name', ' Address', 'Telephone', 'Birthday']
    ['John Konon', 'Ministry of Moon Walks', '4567882', '27-Feb']
    ['Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan']

    But i wanted to make it

    [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Feb'),
    ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]

    can someone show me guidance to this issue

    Thanks all
     
    Jiewei Huang, Mar 24, 2013
    #1
    1. Advertising

  2. Jiewei Huang

    Dave Angel Guest

    On 03/24/2013 01:20 AM, Jiewei Huang wrote:
    > Hi all,
    >
    > Currently create a simple text-based database of information about people
    >
    >
    > I have a csv file which consist of 3 rows , row 1 2 and 3 is as such:
    > Name Address Telephone Birthday
    > John Konon Ministry of Moon Walks 4567882 27-Feb
    > Stacy Kisha Ministry of Man Power 1234567 17-Jan
    >
    >
    > My codes are :
    > import csv
    > original = file('friends.csv', 'rU')
    > reader = csv.reader(original)
    >
    > for row in reader:
    >
    > print row
    >
    >
    > and the output is :
    > ['Name', ' Address', 'Telephone', 'Birthday']
    > ['John Konon', 'Ministry of Moon Walks', '4567882', '27-Feb']
    > ['Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan']
    >
    > But i wanted to make it
    >
    > [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Feb'),
    > ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]
    >
    > can someone show me guidance to this issue
    >
    > Thanks all
    >


    There are at least 3 differences to the printouts. Which difference are
    you concerned with?

    1) Moon --> Silly
    2) square and round brackets in slightly different places
    3) row 0 suppressed

    Explain which one, or specify a fourth, and also what you have tried to
    address the question.


    --
    DaveA
     
    Dave Angel, Mar 24, 2013
    #2
    1. Advertising

  3. Jiewei Huang

    rusi Guest

    On Mar 24, 10:46 am, Dave Angel <> wrote:
    > On 03/24/2013 01:20 AM, Jiewei Huang wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > Hi all,

    >
    > > Currently create a simple text-based database of information about people

    >
    > > I have a csv file which consist of 3 rows , row 1 2 and 3 is as such:
    > > Name        Address        Telephone       Birthday
    > > John Konon Ministry of Moon Walks  4567882 27-Feb
    > > Stacy Kisha        Ministry of Man Power   1234567 17-Jan

    >
    > > My codes are :
    > > import csv
    > > original = file('friends.csv', 'rU')
    > > reader = csv.reader(original)

    >
    > > for row in reader:

    >
    > >      print row

    >
    > > and the output is :
    > > ['Name', ' Address', 'Telephone', 'Birthday']
    > > ['John Konon', 'Ministry of Moon Walks', '4567882', '27-Feb']
    > > ['Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan']

    >
    > > But i wanted to make it

    >
    > > [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Feb'),
    > > ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]

    >
    > > can someone show me guidance to this issue

    >
    > > Thanks all

    >
    > There are at least 3 differences to the printouts.  Which difference are
    > you concerned with?
    >
    > 1) Moon --> Silly
    > 2) square and round brackets in slightly different places
    > 3) row 0 suppressed
    >
    > Explain which one, or specify a fourth, and also what you have tried to
    > address the question.
    >
    > --
    > DaveA


    Besides does it work as you are describing?
    ie your input appears to be tab delimited, whereas the default is ','
     
    rusi, Mar 24, 2013
    #3
  4. Jiewei Huang

    Jiewei Huang Guest

    On Sunday, March 24, 2013 3:46:49 PM UTC+10, Dave Angel wrote:
    > On 03/24/2013 01:20 AM, Jiewei Huang wrote:
    >
    > > Hi all,

    >
    > >

    >
    > > Currently create a simple text-based database of information about people

    >
    > >

    >
    > >

    >
    > > I have a csv file which consist of 3 rows , row 1 2 and 3 is as such:

    >
    > > Name Address Telephone Birthday

    >
    > > John Konon Ministry of Moon Walks 4567882 27-Feb

    >
    > > Stacy Kisha Ministry of Man Power 1234567 17-Jan

    >
    > >

    >
    > >

    >
    > > My codes are :

    >
    > > import csv

    >
    > > original = file('friends.csv', 'rU')

    >
    > > reader = csv.reader(original)

    >
    > >

    >
    > > for row in reader:

    >
    > >

    >
    > > print row

    >
    > >

    >
    > >

    >
    > > and the output is :

    >
    > > ['Name', ' Address', 'Telephone', 'Birthday']

    >
    > > ['John Konon', 'Ministry of Moon Walks', '4567882', '27-Feb']

    >
    > > ['Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan']

    >
    > >

    >
    > > But i wanted to make it

    >
    > >

    >
    > > [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Feb'),

    >
    > > ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]

    >
    > >

    >
    > > can someone show me guidance to this issue

    >
    > >

    >
    > > Thanks all

    >
    > >

    >
    >
    >
    > There are at least 3 differences to the printouts. Which difference are
    >
    > you concerned with?
    >
    >
    >
    > 1) Moon --> Silly
    >
    > 2) square and round brackets in slightly different places
    >
    > 3) row 0 suppressed
    >
    >
    >
    > Explain which one, or specify a fourth, and also what you have tried to
    >
    > address the question.
    >
    >
    >
    >
    >
    > --
    >
    > DaveA



    Sorry my typo in the output here is the correct output that i need :

    [('John Konon', 'Ministry of moon Walks', '4567882', '27-Feb'),
    ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]

    the difference is that i need a [(row two), (row three), (row fouth)] . I do not want to display row one which is ['Name', ' Address', 'Telephone', 'Birthday']
     
    Jiewei Huang, Mar 24, 2013
    #4
  5. Jiewei Huang

    Jiewei Huang Guest

    On Sunday, March 24, 2013 3:46:49 PM UTC+10, Dave Angel wrote:
    > On 03/24/2013 01:20 AM, Jiewei Huang wrote:
    >
    > > Hi all,

    >
    > >

    >
    > > Currently create a simple text-based database of information about people

    >
    > >

    >
    > >

    >
    > > I have a csv file which consist of 3 rows , row 1 2 and 3 is as such:

    >
    > > Name Address Telephone Birthday

    >
    > > John Konon Ministry of Moon Walks 4567882 27-Feb

    >
    > > Stacy Kisha Ministry of Man Power 1234567 17-Jan

    >
    > >

    >
    > >

    >
    > > My codes are :

    >
    > > import csv

    >
    > > original = file('friends.csv', 'rU')

    >
    > > reader = csv.reader(original)

    >
    > >

    >
    > > for row in reader:

    >
    > >

    >
    > > print row

    >
    > >

    >
    > >

    >
    > > and the output is :

    >
    > > ['Name', ' Address', 'Telephone', 'Birthday']

    >
    > > ['John Konon', 'Ministry of Moon Walks', '4567882', '27-Feb']

    >
    > > ['Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan']

    >
    > >

    >
    > > But i wanted to make it

    >
    > >

    >
    > > [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Feb'),

    >
    > > ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]

    >
    > >

    >
    > > can someone show me guidance to this issue

    >
    > >

    >
    > > Thanks all

    >
    > >

    >
    >
    >
    > There are at least 3 differences to the printouts. Which difference are
    >
    > you concerned with?
    >
    >
    >
    > 1) Moon --> Silly
    >
    > 2) square and round brackets in slightly different places
    >
    > 3) row 0 suppressed
    >
    >
    >
    > Explain which one, or specify a fourth, and also what you have tried to
    >
    > address the question.
    >
    >
    >
    >
    >
    > --
    >
    > DaveA



    Sorry my typo in the output here is the correct output that i need :

    [('John Konon', 'Ministry of moon Walks', '4567882', '27-Feb'),
    ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]

    the difference is that i need a [(row two), (row three), (row fouth)] . I do not want to display row one which is ['Name', ' Address', 'Telephone', 'Birthday']
     
    Jiewei Huang, Mar 24, 2013
    #5
  6. Jiewei Huang

    Jiewei Huang Guest

    On Sunday, March 24, 2013 5:34:03 PM UTC+10, rusi wrote:
    > On Mar 24, 10:46 am, Dave Angel <> wrote:
    >
    > > On 03/24/2013 01:20 AM, Jiewei Huang wrote:

    >
    > >

    >
    > >

    >
    > >

    >
    > >this is the data in csv file http://imgur.com/L4qUkGQ

    and this is the correct output that i need to get:
    [('John Konon', 'Ministry of moon Walks', '4567882', '27-Feb'),
    ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]
    sorry for the confusion

    >
    > >

    >
    > >

    >
    > >

    >
    > >

    >
    > >

    >
    > > > Hi all,

    >
    > >

    >
    > > > Currently create a simple text-based database of information about people

    >
    > >

    >
    > > > I have a csv file which consist of 3 rows , row 1 2 and 3 is as such:

    >
    > > > Name        Address        Telephone       Birthday

    >
    > > > John Konon Ministry of Moon Walks  4567882 27-Feb

    >
    > > > Stacy Kisha        Ministry of Man Power   1234567 17-Jan

    >
    > >

    >
    > > > My codes are :

    >
    > > > import csv

    >
    > > > original = file('friends.csv', 'rU')

    >
    > > > reader = csv.reader(original)

    >
    > >

    >
    > > > for row in reader:

    >
    > >

    >
    > > >      print row

    >
    > >

    >
    > > > and the output is :

    >
    > > > ['Name', ' Address', 'Telephone', 'Birthday']

    >
    > > > ['John Konon', 'Ministry of Moon Walks', '4567882', '27-Feb']

    >
    > > > ['Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan']

    >
    > >

    >
    > > > But i wanted to make it

    >
    > >

    >
    > > > [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Feb'),

    >
    > > > ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]

    >
    > >

    >
    > > > can someone show me guidance to this issue

    >
    > >

    >
    > > > Thanks all

    >
    > >

    >
    > > There are at least 3 differences to the printouts.  Which difference are

    >
    > > you concerned with?

    >
    > >

    >
    > > 1) Moon --> Silly

    >
    > > 2) square and round brackets in slightly different places

    >
    > > 3) row 0 suppressed

    >
    > >

    >
    > > Explain which one, or specify a fourth, and also what you have tried to

    >
    > > address the question.

    >
    > >

    >
    > > --

    >
    > > DaveA

    >
    >
    >
    > Besides does it work as you are describing?
    >
    > ie your input appears to be tab delimited, whereas the default is ','
     
    Jiewei Huang, Mar 24, 2013
    #6
  7. Jiewei Huang

    ypsun Guest

    Jiewei Huangæ–¼ 2013å¹´3月24日星期日UTC+1上åˆ6時20分29秒寫é“:
    > Hi all,
    >
    >
    >
    > Currently create a simple text-based database of information about people
    >
    >
    >
    >
    >
    > I have a csv file which consist of 3 rows , row 1 2 and 3 is as such:
    >
    > Name Address Telephone Birthday
    >
    > John Konon Ministry of Moon Walks 4567882 27-Feb
    >
    > Stacy Kisha Ministry of Man Power 1234567 17-Jan
    >
    >
    >
    >
    >
    > My codes are :
    >
    > import csv
    >
    > original = file('friends.csv', 'rU')
    >
    > reader = csv.reader(original)
    >
    >
    >
    > for row in reader:
    >
    >
    >
    > print row
    >
    >
    >
    >
    >
    > and the output is :
    >
    > ['Name', ' Address', 'Telephone', 'Birthday']
    >
    > ['John Konon', 'Ministry of Moon Walks', '4567882', '27-Feb']
    >
    > ['Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan']
    >
    >
    >
    > But i wanted to make it
    >
    >
    >
    > [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Feb'),
    >
    > ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]
    >
    >
    >
    > can someone show me guidance to this issue
    >
    >
    >
    > Thanks all


    import csv
    original = file('friends.csv', 'rU')
    reader = csv.reader(original)

    result = []
    for row in reader:
    result.append(tuple(row))
    else:
    print result[1:]


    Is this what you need?
    but this allocates memory and will be a trouble when you have a big table :p
    Note that I assume your csv file content are comma seperated.
     
    ypsun, Mar 24, 2013
    #7
  8. Jiewei Huang

    ypsun Guest

    Jiewei Huangæ–¼ 2013å¹´3月24日星期日UTC+1上åˆ6時20分29秒寫é“:
    > Hi all,
    >
    >
    >
    > Currently create a simple text-based database of information about people
    >
    >
    >
    >
    >
    > I have a csv file which consist of 3 rows , row 1 2 and 3 is as such:
    >
    > Name Address Telephone Birthday
    >
    > John Konon Ministry of Moon Walks 4567882 27-Feb
    >
    > Stacy Kisha Ministry of Man Power 1234567 17-Jan
    >
    >
    >
    >
    >
    > My codes are :
    >
    > import csv
    >
    > original = file('friends.csv', 'rU')
    >
    > reader = csv.reader(original)
    >
    >
    >
    > for row in reader:
    >
    >
    >
    > print row
    >
    >
    >
    >
    >
    > and the output is :
    >
    > ['Name', ' Address', 'Telephone', 'Birthday']
    >
    > ['John Konon', 'Ministry of Moon Walks', '4567882', '27-Feb']
    >
    > ['Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan']
    >
    >
    >
    > But i wanted to make it
    >
    >
    >
    > [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Feb'),
    >
    > ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]
    >
    >
    >
    > can someone show me guidance to this issue
    >
    >
    >
    > Thanks all


    import csv
    original = file('friends.csv', 'rU')
    reader = csv.reader(original)
    print [tuple(row) for row in reader][1:]

    is this you want?
    Note that:
    1) I assume your csv file content are seperated by comma
    2) this way allocates your memory, will be a trouble when a big table :p
     
    ypsun, Mar 24, 2013
    #8
  9. Jiewei Huang

    Dave Angel Guest

    On 03/24/2013 04:11 AM, Jiewei Huang wrote:
    >
    >> <SNIP all those quoted lines doubled by anti-social googlegroups>
    >>

    >
    > Sorry my typo in the output here is the correct output that i need :
    >
    > [('John Konon', 'Ministry of moon Walks', '4567882', '27-Feb'),
    > ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]
    >
    > the difference is that i need a [(row two), (row three), (row fouth)] . I do not want to display row one which is ['Name', ' Address', 'Telephone', 'Birthday']
    >


    I had to add a delimiter= to accomodate the fact that your data is tab-
    separated.

    The first two code sections address the dropping of row-zero.
    The third section changes the square brackets to parens, if that's what
    you really wanted.
    The fourth section goes all around the barn to get the linefeeds in the
    right place. There's still one extra space on the first line, but
    that's an exercise for the reader.

    run with CPython 2.7.3

    import csv

    original = [
    "Name Address Telephone Birthday ",
    "John Konon Ministry of Moon Walks 4567882 27-Feb",
    "Stacy Kisha Ministry of Man Power 1234567 17-Jan"
    ]

    #original = file('friends.csv', 'rU')
    reader = csv.reader(original, delimiter="\t")
    for index, row in enumerate(reader):
    if index:
    print row
    print "-----------"

    reader = csv.reader(original, delimiter="\t")
    data = list(reader)[1:]
    print data
    print "-----------"

    reader = csv.reader(original, delimiter="\t")
    data = [tuple(row) for row in list(reader)[1:]]
    print data
    print "-----------"

    reader = csv.reader(original, delimiter="\t")
    data = [tuple(row) for row in list(reader)[1:]]
    print "[",
    for row in data[:-1]:
    print str(row) + ","
    print str(data[-1]) +"]"
    print "-----------"


    --
    DaveA
     
    Dave Angel, Mar 24, 2013
    #9
  10. Jiewei Huang

    Tim Chase Guest

    On 2013-03-24 09:03, Dave Angel wrote:
    > >> <SNIP all those quoted lines doubled by anti-social googlegroups>


    [THANK YOU!]

    > > Sorry my typo in the output here is the correct output that i
    > > need :
    > >
    > > [('John Konon', 'Ministry of moon Walks', '4567882', '27-Feb'),
    > > ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]
    > >
    > > the difference is that i need a [(row two), (row three), (row
    > > fouth)] . I do not want to display row one which is ['Name', '
    > > Address', 'Telephone', 'Birthday']

    >
    > I had to add a delimiter= to accomodate the fact that your data is
    > tab- separated.


    Since it has headers, it might make more sense for readability to use
    a DictReader:

    f = file("source.txt", "rb")
    r = csv.DictReader(f, delimiter='\t')
    for row in r:
    print row["Name"], row["Address"], ...

    However, if it's just stripping off the first row and operating
    directly on the resulting data, then I'd just be tempted to do

    r = csv.reader(f, delimiter='\t')
    r.next() # discard the headers, or optionally store them
    for row in r:
    ...

    -tkc
     
    Tim Chase, Mar 24, 2013
    #10
  11. Jiewei Huang

    rusi Guest

    On Mar 24, 6:49 pm, Tim Chase <> wrote:
    > On 2013-03-24 09:03, Dave Angel wrote:
    >
    > > >> <SNIP all those quoted lines doubled by anti-social googlegroups>

    >
    > [THANK YOU!]
    >
    > > > Sorry my typo in the output here is the correct output that i
    > > > need :

    >
    > > > [('John Konon', 'Ministry of moon Walks', '4567882', '27-Feb'),
    > > > ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]

    >
    > > > the difference is that i need a [(row two), (row three), (row
    > > > fouth)] . I do not want to display row one which is ['Name', '
    > > > Address', 'Telephone', 'Birthday']

    >
    > > I had to add a delimiter= to accomodate the fact that your data is
    > > tab- separated.

    >
    > Since it has headers, it might make more sense for readability to use
    > a DictReader:
    >
    >   f = file("source.txt", "rb")
    >   r = csv.DictReader(f, delimiter='\t')
    >   for row in r:
    >     print row["Name"], row["Address"], ...
    >
    > However, if it's just stripping off the first row and operating
    > directly on the resulting data, then I'd just be tempted to do
    >
    >   r = csv.reader(f, delimiter='\t')
    >   r.next() # discard the headers, or optionally store them
    >   for row in r:
    >     ...
    >
    > -tkc


    After doing:

    >>> import csv
    >>> original = file('friends.csv', 'rU')
    >>> reader = csv.reader(original, delimiter='\t')



    Stripping of the first line is:
    >>> list(reader)[1:]


    If for some reason you want to tuplify each row do either:
    >>> [tuple(row) for row in list(reader)[1:]]


    Or else
    >>> map(tuple,list(reader)[1:])


    I usually prefer comprehensions to maps. Here though, the map is
    shorter. Your choice


    Then you can of course make your code more performant thus:
    >>> reader.next()
    >>> (tuple(row) for row in reader)


    In the majority of cases this optimization is not worth it
    In any case, strewing prints all over the code is a bad habit (except
    for debugging).
     
    rusi, Mar 24, 2013
    #11
  12. Jiewei Huang

    Tim Chase Guest

    On 2013-03-24 08:57, rusi wrote:
    > On Mar 24, 6:49 pm, Tim Chase <> wrote:
    > After doing:
    >
    > >>> import csv
    > >>> original = file('friends.csv', 'rU')
    > >>> reader = csv.reader(original, delimiter='\t')

    >
    >
    > Stripping of the first line is:
    > >>> list(reader)[1:]
    > >>> [tuple(row) for row in list(reader)[1:]]
    > >>> map(tuple,list(reader)[1:])


    This works for small sources, but slurps all the data into memory.
    Because csv.reader is an iterator/generator, it can process huge CSV
    files that wouldn't otherwise fit in memory. By using either
    r.next() (or "next(r)" in newer versions), it fetches one record from
    the generator, to be discarded/stored as appropriate.


    > Then you can of course make your code more performant thus:
    > >>> reader.next()
    > >>> (tuple(row) for row in reader)

    >
    > In the majority of cases this optimization is not worth it


    If the CSV file is large, using the iterator version is usually worth
    the small performance penalty, as you don't have to keep the whole
    file in memory. As somebody who regularly deals with 0.5-1GB CSV
    files from cellular providers, I speak from experience of having my
    machine choke when reading the whole thing in.

    > In any case, strewing prints all over the code is a bad habit
    > (except for debugging).


    Sorry if my print-statements were misinterpreted--I meant them as a
    "do what you want with the data here" stand-in (thus the ellipsis).

    -tkc
     
    Tim Chase, Mar 24, 2013
    #12
  13. Jiewei Huang

    Jiewei Huang Guest

    On Sunday, March 24, 2013 3:20:29 PM UTC+10, Jiewei Huang wrote:

    thanks to you guys i got my desire outcome! i will continue to code on writing and saving of csv file
     
    Jiewei Huang, Mar 25, 2013
    #13
  14. Jiewei Huang

    rusi Guest

    On Mar 24, 11:28 pm, Tim Chase <> wrote:
    >
    > Sorry if my print-statements were misinterpreted--I meant them as a
    > "do what you want with the data here" stand-in (thus the ellipsis).


    Heh! I assumed the OP was a noob to whom this was directed (and whose
    original had the print statements in loops).

    > As somebody who regularly deals with 0.5-1GB CSV
    > files from cellular providers, I speak from experience of having my
    > machine choke when reading the whole thing in.


    If the 'friends.csv' of the OP goes up to 1GB... Now thats gregarious!
     
    rusi, Mar 25, 2013
    #14
  15. Jiewei Huang

    Jiewei Huang Guest

    On Sunday, March 24, 2013 9:10:45 PM UTC+10, ypsun wrote:
    > Jiewei Huangæ–¼ 2013å¹´3月24日星期日UTC+1上åˆ6時20分29秒寫é“:
    >
    > > Hi all,

    >
    > >

    >
    > >

    >
    > >

    >
    > > Currently create a simple text-based database of information about people

    >
    > >

    >
    > >

    >
    > >

    >
    > >

    >
    > >

    >
    > > I have a csv file which consist of 3 rows , row 1 2 and 3 is as such:

    >
    > >

    >
    > > Name Address Telephone Birthday

    >
    > >

    >
    > > John Konon Ministry of Moon Walks 4567882 27-Feb

    >
    > >

    >
    > > Stacy Kisha Ministry of Man Power 1234567 17-Jan

    >
    > >

    >
    > >

    >
    > >

    >
    > >

    >
    > >

    >
    > > My codes are :

    >
    > >

    >
    > > import csv

    >
    > >

    >
    > > original = file('friends.csv', 'rU')

    >
    > >

    >
    > > reader = csv.reader(original)

    >
    > >

    >
    > >

    >
    > >

    >
    > > for row in reader:

    >
    > >

    >
    > >

    >
    > >

    >
    > > print row

    >
    > >

    >
    > >

    >
    > >

    >
    > >

    >
    > >

    >
    > > and the output is :

    >
    > >

    >
    > > ['Name', ' Address', 'Telephone', 'Birthday']

    >
    > >

    >
    > > ['John Konon', 'Ministry of Moon Walks', '4567882', '27-Feb']

    >
    > >

    >
    > > ['Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan']

    >
    > >

    >
    > >

    >
    > >

    >
    > > But i wanted to make it

    >
    > >

    >
    > >

    >
    > >

    >
    > > [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Feb'),

    >
    > >

    >
    > > ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]

    >
    > >

    >
    > >

    >
    > >

    >
    > > can someone show me guidance to this issue

    >
    > >

    >
    > >

    >
    > >

    >
    > > Thanks all

    >
    >
    >
    > import csv
    >
    > original = file('friends.csv', 'rU')
    >
    > reader = csv.reader(original)
    >
    > print [tuple(row) for row in reader][1:]
    >
    >
    >
    > is this you want?
    >
    > Note that:
    >
    > 1) I assume your csv file content are seperated by comma
    >
    > 2) this way allocates your memory, will be a trouble when a big table:p


    Hi guys, i got into another situation i was told not to use csv library andcurrently my code is :

    f = open('friends.csv', 'rU')
    for row in f:

    print [row for (row) in f]

    output :
    ['John Cleese,Ministry of Silly Walks,5555421,27-Oct\n', 'Stacy Kisha,Ministry of Man Power,1234567,17-Jan\n']

    i need it to become :
    [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Oct'), ('Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan')]

    guide me please
     
    Jiewei Huang, Mar 25, 2013
    #15
  16. Jiewei Huang

    rusi Guest

    On Mar 25, 11:52 am, Jiewei Huang <> wrote:
    > On Sunday, March 24, 2013 9:10:45 PM UTC+10, ypsun wrote:
    > > Jiewei Huangæ–¼ 2013å¹´3月24日星期日UTC+1上åˆ6時20分29秒寫é“:

    >
    > > > Hi all,

    >
    > > > Currently create a simple text-based database of information about people

    >
    > > > I have a csv file which consist of 3 rows , row 1 2 and 3 is as such:

    >
    > > > Name   Address        Telephone       Birthday

    >
    > > > John Konon    Ministry of Moon Walks  4567882 27-Feb

    >
    > > > Stacy Kisha   Ministry of Man Power   1234567 17-Jan

    >
    > > > My codes are :

    >
    > > > import csv

    >
    > > > original = file('friends.csv', 'rU')

    >
    > > > reader = csv.reader(original)

    >
    > > > for row in reader:

    >
    > > >     print row

    >
    > > > and the output is :

    >
    > > > ['Name', ' Address', 'Telephone', 'Birthday']

    >
    > > > ['John Konon', 'Ministry of Moon Walks', '4567882', '27-Feb']

    >
    > > > ['Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan']

    >
    > > > But i wanted to make it

    >
    > > > [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Feb'),

    >
    > > > ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]

    >
    > > > can someone show me guidance to this issue

    >
    > > > Thanks all

    >
    > > import csv

    >
    > > original = file('friends.csv', 'rU')

    >
    > > reader = csv.reader(original)

    >
    > > print [tuple(row) for row in reader][1:]

    >
    > > is this you want?

    >
    > > Note that:

    >
    > >     1) I assume your csv file content are seperated by comma

    >
    > >     2) this way allocates your memory, will be a trouble whena big table :p

    >
    > Hi guys, i got into another situation i was told not to use csv library and currently my code is :
    >
    > f = open('friends.csv', 'rU')
    > for row in f:
    >
    >         print [row for (row) in f]
    >
    > output :
    > ['John Cleese,Ministry of Silly Walks,5555421,27-Oct\n', 'Stacy Kisha,Ministry of Man Power,1234567,17-Jan\n']
    >
    > i need it to become :
    > [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Oct'), ('StacyKisha', 'Ministry of Man Power', '1234567', '17-Jan')]
    >
    > guide me please


    Have you tried the split (and perhaps strip) methods from
    http://docs.python.org/2/library/stdtypes.html#string-methods
    ?
     
    rusi, Mar 25, 2013
    #16
  17. Jiewei Huang

    Jiewei Huang Guest

    On Monday, March 25, 2013 11:51:51 PM UTC+10, rusi wrote:
    > On Mar 25, 11:52 am, Jiewei Huang <> wrote:
    >
    > > On Sunday, March 24, 2013 9:10:45 PM UTC+10, ypsun wrote:

    >
    > > > Jiewei Huangæ–¼ 2013å¹´3月24日星期日UTC+1上åˆ6時20分29秒寫é“:

    >
    > >

    >
    > > > > Hi all,

    >
    > >

    >
    > > > > Currently create a simple text-based database of information about people

    >
    > >

    >
    > > > > I have a csv file which consist of 3 rows , row 1 2 and 3 is as such:

    >
    > >

    >
    > > > > Name   Address        Telephone       Birthday

    >
    > >

    >
    > > > > John Konon    Ministry of Moon Walks  4567882 27-Feb

    >
    > >

    >
    > > > > Stacy Kisha   Ministry of Man Power   1234567 17-Jan

    >
    > >

    >
    > > > > My codes are :

    >
    > >

    >
    > > > > import csv

    >
    > >

    >
    > > > > original = file('friends.csv', 'rU')

    >
    > >

    >
    > > > > reader = csv.reader(original)

    >
    > >

    >
    > > > > for row in reader:

    >
    > >

    >
    > > > >     print row

    >
    > >

    >
    > > > > and the output is :

    >
    > >

    >
    > > > > ['Name', ' Address', 'Telephone', 'Birthday']

    >
    > >

    >
    > > > > ['John Konon', 'Ministry of Moon Walks', '4567882', '27-Feb']

    >
    > >

    >
    > > > > ['Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan']

    >
    > >

    >
    > > > > But i wanted to make it

    >
    > >

    >
    > > > > [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Feb'),

    >
    > >

    >
    > > > > ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]

    >
    > >

    >
    > > > > can someone show me guidance to this issue

    >
    > >

    >
    > > > > Thanks all

    >
    > >

    >
    > > > import csv

    >
    > >

    >
    > > > original = file('friends.csv', 'rU')

    >
    > >

    >
    > > > reader = csv.reader(original)

    >
    > >

    >
    > > > print [tuple(row) for row in reader][1:]

    >
    > >

    >
    > > > is this you want?

    >
    > >

    >
    > > > Note that:

    >
    > >

    >
    > > >     1) I assume your csv file content are seperated by comma

    >
    > >

    >
    > > >     2) this way allocates your memory, will be a trouble when a big table :p

    >
    > >

    >
    > > Hi guys, i got into another situation i was told not to use csv libraryand currently my code is :

    >
    > >

    >
    > > f = open('friends.csv', 'rU')

    >
    > > for row in f:

    >
    > >

    >
    > >         print [row for (row) in f]

    >
    > >

    >
    > > output :

    >
    > > ['John Cleese,Ministry of Silly Walks,5555421,27-Oct\n', 'Stacy Kisha,Ministry of Man Power,1234567,17-Jan\n']

    >
    > >

    >
    > > i need it to become :

    >
    > > [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Oct'), ('Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan')]

    >
    > >

    >
    > > guide me please

    >
    >
    >
    > Have you tried the split (and perhaps strip) methods from
    >
    > http://docs.python.org/2/library/stdtypes.html#string-methods
    >
    > ?

    can show me one line of how to implement it base on my problem?
     
    Jiewei Huang, Mar 26, 2013
    #17
  18. Jiewei Huang

    Dave Angel Guest

    On 03/25/2013 09:05 PM, Jiewei Huang wrote:
    > On Monday, March 25, 2013 11:51:51 PM UTC+10, rusi wrote:


    If you insist on using GoogleGroups, then make sure you keep your quotes
    small. I'm about to stop reading messages that are double-spaced by
    buggy software.

    >>> <SNIP>

    >>


    >>
    >>
    >> Have you tried the split (and perhaps strip) methods from
    >>
    >> http://docs.python.org/2/library/stdtypes.html#string-methods
    >>
    >> ?


    You got lots of specific advice from your previous thread. So which
    version did you end up using? It'd make a good starting place for this
    "problem."



    > can show me one line of how to implement it base on my problem?
    >


    As long as the input data is constrained not to have any embedded
    commas, just use:

    mylist = line.split(",")


    instead of print, send your output to a list. Then for each line in the
    list, fix the bracket problem to your strange specs.

    outline = outline.replace("[", "(")

    --
    --
    DaveA
     
    Dave Angel, Mar 26, 2013
    #18
  19. Jiewei Huang

    Jiewei Huang Guest

    On Tuesday, March 26, 2013 11:40:51 AM UTC+10, Dave Angel wrote:
    > On 03/25/2013 09:05 PM, Jiewei Huang wrote:
    >
    > > On Monday, March 25, 2013 11:51:51 PM UTC+10, rusi wrote:

    >
    >
    >
    > If you insist on using GoogleGroups, then make sure you keep your quotes
    >
    > small. I'm about to stop reading messages that are double-spaced by
    >
    > buggy software.
    >
    >
    >
    > >>> <SNIP>

    >
    > >>

    >
    >
    >
    > >>

    >
    > >>

    >
    > >> Have you tried the split (and perhaps strip) methods from

    >
    > >>

    >
    > >> http://docs.python.org/2/library/stdtypes.html#string-methods

    >
    > >>

    >
    > >> ?

    >
    >
    >
    > You got lots of specific advice from your previous thread. So which
    >
    > version did you end up using? It'd make a good starting place for this
    >
    > "problem."
    >
    >
    >
    >
    >
    >
    >
    > > can show me one line of how to implement it base on my problem?

    >
    > >

    >
    >
    >
    > As long as the input data is constrained not to have any embedded
    >
    > commas, just use:
    >
    >
    >
    > mylist = line.split(",")
    >
    >
    >
    >
    >
    > instead of print, send your output to a list. Then for each line in the
    >
    > list, fix the bracket problem to your strange specs.
    >
    >
    >
    > outline = outline.replace("[", "(")
    >
    >
    >
    > --
    >
    > --
    >
    > DaveA


    Hi Dave thanks for the tips,

    I manage to code this:
    f = open('Book1.csv', 'rU')
    for row in f:
    print zip([row for (row) in f])

    however my output is
    [('John Konon Ministry of Moon Walks 4567882 27-Feb\n',), ('Stacy Kisha Ministry of Man Power 1234567 17-Jan\n',)]

    is there any method to remove the \n ?
     
    Jiewei Huang, Mar 26, 2013
    #19
  20. Jiewei Huang

    Jiewei Huang Guest

    On Tuesday, March 26, 2013 11:40:51 AM UTC+10, Dave Angel wrote:
    > On 03/25/2013 09:05 PM, Jiewei Huang wrote:
    >
    > > On Monday, March 25, 2013 11:51:51 PM UTC+10, rusi wrote:

    >
    >
    >
    > If you insist on using GoogleGroups, then make sure you keep your quotes
    >
    > small. I'm about to stop reading messages that are double-spaced by
    >
    > buggy software.
    >
    >
    >
    > >>> <SNIP>

    >
    > >>

    >
    >
    >
    > >>

    >
    > >>

    >
    > >> Have you tried the split (and perhaps strip) methods from

    >
    > >>

    >
    > >> http://docs.python.org/2/library/stdtypes.html#string-methods

    >
    > >>

    >
    > >> ?

    >
    >
    >
    > You got lots of specific advice from your previous thread. So which
    >
    > version did you end up using? It'd make a good starting place for this
    >
    > "problem."
    >
    >
    >
    >
    >
    >
    >
    > > can show me one line of how to implement it base on my problem?

    >
    > >

    >
    >
    >
    > As long as the input data is constrained not to have any embedded
    >
    > commas, just use:
    >
    >
    >
    > mylist = line.split(",")
    >
    >
    >
    >
    >
    > instead of print, send your output to a list. Then for each line in the
    >
    > list, fix the bracket problem to your strange specs.
    >
    >
    >
    > outline = outline.replace("[", "(")
    >
    >
    >
    > --
    >
    > --
    >
    > DaveA


    Hi Dave thanks for the tips,

    I manage to code this:
    f = open('Book1.csv', 'rU')
    for row in f:
    print zip([row for (row) in f])

    however my output is
    [('John Konon Ministry of Moon Walks 4567882 27-Feb\n',), ('Stacy Kisha Ministry of Man Power 1234567 17-Jan\n',)]

    is there any method to remove the \n ?
     
    Jiewei Huang, Mar 26, 2013
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. EvgueniB
    Replies:
    1
    Views:
    637
    Anthony Borla
    Dec 15, 2003
  2. Frank Fredstone
    Replies:
    1
    Views:
    448
    Jean-Francois Briere
    Jun 27, 2006
  3. =?Utf-8?B?SmltIEhlYXZleQ==?=

    Data Reader Now Rows

    =?Utf-8?B?SmltIEhlYXZleQ==?=, Aug 14, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    433
    =?Utf-8?B?SmltIEhlYXZleQ==?=
    Aug 16, 2006
  4. Guilherme Grillo

    reader inside a reader

    Guilherme Grillo, Nov 7, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    528
    sloan
    Nov 7, 2007
  5. Pokkai Dokkai
    Replies:
    1
    Views:
    237
    Hassan Schroeder
    Mar 24, 2008
Loading...

Share This Page