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


Groups > comp.lang.python > #54360

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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'output': 0.05; 'column': 0.07; 'subject:adding': 0.07; 'handful': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'assume': 0.14; "'.txt',": 0.16; "'\\n')": 0.16; "'rb')": 0.16; "'w')": 0.16; '.txt': 0.16; 'appreciated!': 0.16; 'bryan': 0.16; 'csv': 0.16; 'empty.': 0.16; 'guess.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:.txt': 0.16; 'subject:after': 0.16; 'varnames': 0.16; 'files.': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'trying': 0.19; 'input': 0.22; 'import': 0.22; '(in': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'rid': 0.24; 'script.': 0.24; 'header': 0.24; 'looks': 0.24; "i've": 0.25; 'header:X-Complaints-To:1': 0.27; 'skip:g 30': 0.30; "i'm": 0.30; 'code': 0.31; 'lines': 0.31; "skip:' 10": 0.31; 'file': 0.32; 'run': 0.32; 'another': 0.32; 'reader': 0.33; "i'd": 0.34; 'problem': 0.35; '(2)': 0.35; 'but': 0.35; 'add': 0.35; 'two': 0.37; 'being': 0.38; 'loss': 0.38; 'skip:o 20': 0.38; '(3)': 0.38; 'filter': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'issue': 0.38; 'previous': 0.38; 'generating': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'remove': 0.60; 'is.': 0.60; 'manually': 0.60; 'new': 0.61; 'finally': 0.65; 'here': 0.66; 'date,': 0.68; 'results': 0.69; 'export': 0.74; 'hey,': 0.75; "skip:' 180": 0.84; 'thrown,': 0.84; 'time)': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: *.csv to *.txt after adding columns
Date Wed, 18 Sep 2013 09:14:23 +0200
Organization None
References <41edba54-31d3-48ad-a50f-41f87f32d251@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084b6fc.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
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.106.1379488457.18130.python-list@python.org> (permalink)
Lines 61
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1379488457 news.xs4all.nl 15899 [2001:888:2000:d::a6]:37441
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:54360

Show key headers only | View raw


Bryan Britten wrote:

> Hey, gang, I've got a problem here that I'm sure a handful of you will
> know how to solve. I've got about 6 *.csv files that I am trying to open;
> change the header names (to get rid of spaces); add two new columns, which
> are just the results of a string.split() command; drop the column I just
> split; and then finally export to *.txt files. Here's the code I'm using:
> 
> import os
> import csv
> 
> 
> fileHandle = 'Path/To/Data'
> 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'
> 

You may be processing not just the .csv files, but also the .txt files from 
a previous run of your script. Remove them manually to verify my guess.
If I'm right you can (in order of my preference)

(1) write the output into another directory and also do (2)
(2) filter the input files with glob.glob("/path/to/data/*.csv") 
(3) or just add the two lines below to your current code.

> for csvFile in os.listdir(fileHandle):

      if not csvFile.endswith(".csv"):
          continue

>     outFile = open(fileHandle + os.path.splitext(csvFile)[0] + '.txt',
>     'w') 
>     inFile = open(fileHandle + csvFile, 'rb')
>     reader = csv.reader(inFile, delimiter=',')
>     rowNum = 0
>     for row in reader:
>         if rowNum == 0:
>             outFile.write(varNames)
>             rowNum += 1
>         else:
>             date, time = row[2].split()
>             row.insert(3, date)
>             row.insert(4, time)
>             row.remove(row[2])
>             outFile.write('\t'.join(row) + '\n')
>     outFile.close()
>     inFile.close()
> 
> 
> The issue I'm having is that the *.txt files I'm generating are empty. I
> assume some unraised error is being thrown, but I'm new to Python and am
> self taught, so I don't know where to look or how to troubleshoot.
> 
> When I run the code on just one file and print the output instead of
> writing it, it looks exactly like what I'd want. So I'm at a loss for
> where the problem is.
> 
> Any help is appreciated!

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