Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54362

Re: *.csv to *.txt after adding columns

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'output': 0.05; 'debugging': 0.07; 'see.': 0.07; 'subject:adding': 0.07; 'lines.': 0.09; 'prefix': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'statements': 0.09; 'python': 0.11; 'useful,': 0.14; '"+"': 0.16; "'rb')": 0.16; "'w')": 0.16; 'bryan': 0.16; 'csv': 0.16; 'input:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'slash.': 0.16; 'subject:.txt': 0.16; 'subject:after': 0.16; 'varnames': 0.16; 'exception': 0.16; 'files.': 0.16; 'wrote:': 0.18; 'user.': 0.19; 'code,': 0.22; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'necessary.': 0.24; 'switched': 0.24; 'why.': 0.24; 'file.': 0.24; 'push': 0.26; 'second': 0.26; 'header:X-Complaints-To:1': 0.27; "doesn't": 0.30; 'errors': 0.30; 'said,': 0.30; 'especially': 0.30; "i'm": 0.30; 'included': 0.31; 'code': 0.31; 'correctly.': 0.31; 'terminate': 0.31; 'file': 0.32; 'probably': 0.32; 'run': 0.32; 'another': 0.32; 'everyone': 0.33; 'not.': 0.33; "i'd": 0.34; 'could': 0.34; "can't": 0.35; 'something': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'raising': 0.36; "didn't": 0.36; 'method': 0.36; 'useful': 0.36; 'charset:us-ascii': 0.36; 'changing': 0.37; 'half': 0.37; 'wrong': 0.37; 'ends': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'resource': 0.38; 'expect': 0.39; 'does': 0.39; 'extremely': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'dave': 0.60; 'new': 0.61; "you're": 0.61; 'first': 0.61; 'such': 0.63; 'sample': 0.67; 'close': 0.67; 'records.': 0.68; 'containing': 0.69; 'limit': 0.70; 'records': 0.73; 'code):': 0.84; 'lastly,': 0.84; 'results,': 0.84; "skip:' 180": 0.84; '1,000,000': 0.91; 'dozen': 0.91; 'inefficient': 0.91; 'same,': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dave Angel <davea@davea.name>
Subject Re: *.csv to *.txt after adding columns
Date Wed, 18 Sep 2013 07:55:52 +0000 (UTC)
References <41edba54-31d3-48ad-a50f-41f87f32d251@googlegroups.com> <c7b5595f-b847-4172-9a1d-86287e0804d5@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 174.32.174.35
User-Agent XPN/1.2.6 (Street Spirit ; Linux)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.107.1379490982.18130.python-list@python.org> (permalink)
Lines 63
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1379490982 news.xs4all.nl 15962 [2001:888:2000:d::a6]:46945
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:54362

Show key headers only | View raw


On 17/9/2013 22:28, Bryan Britten wrote:

> Dave -
>
> I can't print the output because there are close to 1,000,000 records. It would be extremely inefficient and resource intensive to look at every row.

Not if you made a sample directory with about 3 files, each containing
half a dozen lines.

> Like I said, when I take just one file and run the code over the first
> few records I get what I'd expect to see. Here's an example(non-redacted code):
>
> INPUT:
>
> import csv
>
> fileHandle = 'C:/Users/Bryan/Data Analysis/Crime Analysis/Data/'

Now, that directory specification ends with a slash.  So "+" will work
correctly.  But your original did not.

>
> varNames = 'ID\tCaseNum\tDate\tTime\tBlock\tIUCR\tPrimaryType\tDescription\tLocDesc\tArrest\tDomestic\tBeat\tDistrict\tWard\tCommArea\tFBICode\tXCoord\tYCoord\tYear\tUpdatedOn\tLat\tLong\tLoc\n'
>
> outFile = open(fileHandle + 'ChiCrime01_02.txt', 'w')
> inFile = open(fileHandle + 'ChiCrime01_02.csv', 'rb')

Instead of changing this code, you could have switched to a directory
containing only one file.

>
> Like I said, the output is exactly what I want, but it doesn't seem to be writing to the file and I don't know why. I said I didn't know if it was raising an exception because I'm new to Python and I didn't know if there were some methods that included "silent" errors where it would continue the code but produce the wrong results, such as not writing my files. 
>

The only "silent" exception I know of is the one triggered by
sys.exit(), used to terminate the process.

> Lastly, why does everyone seem to push for os.path.join versus the method I have used? Is it just a 'standard' that people like to see?
>

Because os.path.join will be smart enough to add the slashes only if
they are necessary.   That can be useful, especially if the directory
you're using as a prefix came from the user.


I think Peter's suggestion is probably right on;  you don't limit your
infiles to *.csv, so you will be processing *.txt files the second time.

Another useful debugging aid would have been to add print statements
right after opening the files, perhaps something like:

print "Reading :", infile.name
print "Writing:", outfile.name

If those names had been missing slashes, I would have been vindicated,
while if they were the same, you'd know Peter had nailed it.



-- 
DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

*.csv to *.txt after adding columns Bryan Britten <britten.bryan@gmail.com> - 2013-09-17 18:42 -0700
  Re: *.csv to *.txt after adding columns Dave Angel <davea@davea.name> - 2013-09-18 02:18 +0000
  Re: *.csv to *.txt after adding columns Bryan Britten <britten.bryan@gmail.com> - 2013-09-17 19:28 -0700
    Re: *.csv to *.txt after adding columns Dave Angel <davea@davea.name> - 2013-09-18 07:55 +0000
  Re: *.csv to *.txt after adding columns Peter Otten <__peter__@web.de> - 2013-09-18 09:14 +0200
  Re: *.csv to *.txt after adding columns rusi <rustompmody@gmail.com> - 2013-09-18 00:44 -0700
  Re: *.csv to *.txt after adding columns Bryan Britten <britten.bryan@gmail.com> - 2013-09-18 04:42 -0700
  Re: *.csv to *.txt after adding columns Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-09-18 18:47 -0400

csiph-web