Need help on python 2.7 regarding splicing of row in csv file

jiewei

Arch-Supremacy Member
Joined
Oct 21, 2006
Messages
11,970
Reaction score
0
Hi all,

Currently i'm creating 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


But i wanted to make it

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

I manage to code out the things i want but was told not to use csv library because it will make the assignment become too easy.

My starting code was:
import csv
def loan_friends(filename='friends.csv'):
original = file('friends.csv', 'rU')
reader = csv.reader(original)
for row in reader:

print [tuple(row) for row in reader]

so now my code is :
f = open('friends.csv', 'rU')
for row in f:

print [row for (row) in f]



for row in reader:

print row

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


i need to make it to
Code:
[('John Cleese', 'Ministry of Moon Walks', '4567882', '27-Feb'), 
( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]
can someone show me guidance to this issue

Thanks all
 

stupidbodo

Junior Member
Joined
Mar 26, 2010
Messages
89
Reaction score
0
Use this

Code:
f = open('Book1.csv', 'rU')
for row in f:
    print zip([row for (row) in f])


Output

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

jiewei

Arch-Supremacy Member
Joined
Oct 21, 2006
Messages
11,970
Reaction score
0
Use this

Code:
f = open('Book1.csv', 'rU')
for row in f:
    print zip([row for (row) in f])


Output

Code:
[('John Konon Ministry of Moon Walks 4567882 27-Feb\n',), ('Stacy Kisha Ministry of Man Power 1234567 17-Jan\n',)]
how to remove the \n ?
 

jiewei

Arch-Supremacy Member
Joined
Oct 21, 2006
Messages
11,970
Reaction score
0
Code:
print zip([row.rstrip() for (row) in f])

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

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

i need ' ' in all the row and need the , to be remove after the date
 
Last edited:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
Code:
[('John Cleese,Ministry of Silly Walks,5555421,27-Oct',), ('Stacy Kisha,Ministry of Man Power,1234567,17-Jan',)]
how to make it to

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

i need ' ' in all the row and need the , to be remove after the date

Quote your csv source if you want 'John Cleese' as one field itself.
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top