Phrasing a csv file into text file

This is the place for queries that don't fit in any of the other categories.

Phrasing a csv file into text file

Postby zhangz64 » Thu Feb 06, 2014 12:08 am

Hi, every one.

I am a second year EE student.
I just started learning python for my project.

I intend to parse a csv file with a format like
Code: Select all
3520005,"Toronto (Ont.)",C  ,F,2503281,2481494,F,F,0.9,1040597,979330,630.1763,3972.4,1
2466023,"Montréal (Que.)",V  ,F,1620693,1583590,T,F,2.3,787060,743204,365.1303,4438.7,2
5915022,"Vancouver (B.C.)",CY ,F,578041,545671,F,F,5.9,273804,253212,114.7133,5039.0,8
3519038,"Richmond Hill (Ont.)",T  ,F,162704,132030,F,F,23.2,53028,51000,100.8917,1612.7,28


into a text file like the following
Code: Select all
Toronto 2503281
Montreal 1620693
Vancouver 578041


I am extracting the 1st and 5th column and save it into a text file.

This is what i have so far.
Code: Select all
import csv
file = open('raw.csv')
reader = csv.reader(file)

f = open('NicelyDone.text','w')

for line in reader:
      f.write("%s %s"%line[1],%line[5])


This is not working for me, I was able to extract the data from the csv file as line[1],line[5]. (I am able to print it out)
But I dont know how to write it to a .text file in the format i wanted.

Also, I have to process the first column eg, "Toronto (Ont.)" into "Toronto".
I am familiar with the function find(), I assume that i could extract Toronto out of Toronto(Ont.) using "(" as the stopping character,
but based on my research , I have no idea how to use it and ask it to return me the string(Toronto).

Here is my question:
1:What is the data format for line[1], if it is string how come f.write()does not work. if it is not string, how do i convert it to a string?
2:How do i extract the word Toronto out of Toronto(Ont) into a string form using find() or other methods.

My thinking is that I could add those 2 string together like c=a+' ' +b, that would give me the format i wanted.
So i can use f.write() to write into a file ;)

Sorry if my questions sounds too easy or stupid.

Thanks ahead

Zhen
Last edited by Yoriz on Thu Feb 06, 2014 12:38 am, edited 1 time in total.
Reason: First post lock
zhangz64
 
Posts: 1
Joined: Wed Feb 05, 2014 11:35 pm

Re: Phrasing a csv file into text file

Postby snippsat » Thu Feb 06, 2014 3:14 am

1:What is the data format for line[1], if it is string how come f.write()does not work. if it is not string, how do i convert it to a string?

It's always a string when read from a file.
2:How do i extract the word Toronto out of Toronto(Ont) into a string form using find() or other methods.

Code: Select all
>>> s = 'Toronto (Ont.)'
>>> s.split()
['Toronto', '(Ont.)']
>>> s.split()[0]
'Toronto'

My thinking is that I could add those 2 string together like c=a+' ' +b, that would give me the format i wanted.
So i can use f.write() to write into a file

Test.
Code: Select all
import csv

with open('raw.csv', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        print row[1].split()[0], row[5]

'''Output--->
Toronto 2481494
Montréal 1583590
Vancouver 545671
Richmond 132030
'''

Write to file with same looking output.
Code: Select all
import csv

with open('raw.csv', 'rb') as f,open('outfile.txt', 'w') as f_out:
    reader = csv.reader(f)
    for row in reader:
        f_out.write('{} {}\n'.format(row[1].split()[0], row[5]))
User avatar
snippsat
 
Posts: 296
Joined: Thu Feb 21, 2013 12:04 am

Re: Phrasing a csv file into text file

Postby stranac » Thu Feb 06, 2014 10:37 am

Just as an additional not, what you were trying was almost correct.
You just got the formatting syntax slightly messed up. It should have been:
Code: Select all
f.write("%s %s" % (line[1], line[5]))
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 4 guests