Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #54405
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!193.141.40.65.MISMATCH!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'else:': 0.03; '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; '"file': 0.16; "'.txt',": 0.16; "'\\n')": 0.16; "'rb')": 0.16; "'w')": 0.16; 'bryan': 0.16; 'csv': 0.16; 'file"': 0.16; 'message-id:@4ax.com': 0.16; 'path.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:.txt': 0.16; 'subject:after': 0.16; 'files.': 0.16; 'module': 0.19; 'trying': 0.19; 'seems': 0.21; 'rid': 0.24; 'url:home': 0.24; 'header': 0.24; "i've": 0.25; 'header:X -Complaints-To:1': 0.27; 'idea': 0.28; "i'm": 0.30; 'code': 0.31; "skip:' 10": 0.31; 'apparently': 0.31; 'sep': 0.31; 'reader': 0.33; "i'd": 0.34; 'problem': 0.35; 'add': 0.35; 'charset:us- ascii': 0.36; 'two': 0.37; 'easily': 0.37; 'received:76': 0.38; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'free': 0.61; 'new': 0.61; 'finally': 0.65; 'here': 0.66; 'capable': 0.67; 'date,': 0.68; 'results': 0.69; 'export': 0.74; 'fin': 0.84; "skip:' 180": 0.84; 'time)': 0.91; '2013': 0.98 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
| Subject | Re: *.csv to *.txt after adding columns |
| Date | Wed, 18 Sep 2013 18:47:40 -0400 |
| Organization | IISS Elusive Unicorn |
| References | <41edba54-31d3-48ad-a50f-41f87f32d251@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | adsl-76-249-21-145.dsl.klmzmi.sbcglobal.net |
| X-Newsreader | Forte Agent 6.00/32.1186 |
| X-No-Archive | YES |
| 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.140.1379544469.18130.python-list@python.org> (permalink) |
| Lines | 78 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1379544470 news.xs4all.nl 15961 [2001:888:2000:d::a6]:58905 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:54405 |
Show key headers only | View raw
On Tue, 17 Sep 2013 18:42:21 -0700 (PDT), Bryan Britten
<britten.bryan@gmail.com> declaimed the following:
>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'
That seems a misnomer... It is not a "file handle" of any sort, just a
base path.
>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'
>
>for csvFile in os.listdir(fileHandle):
> outFile = open(fileHandle + os.path.splitext(csvFile)[0] + '.txt', 'w')
Note that your "fileHandle" does not end with a / -- so I have no idea
what types of names you are trying to open...
Recommend you use
os.path.join(fileHande, os.path.splitext(csvFile)[0] + ".txt")
> inFile = open(fileHandle + csvFile, 'rb')
ditto
> 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')
Apparently your "text file" is a tab-separated file...
The CSV module is capable of processing TSV just as easily as CSV.
I'd also drop the whole confusion of rowNum
-=-=-=-=- pseudo-code, not runnable
fin = open(os.path.join(basepath, filename), "rb")
csvin = csv.reader(fin, delimiter=",")
fout = open(os.path.join(basepath, otherfilename), "wb")
csvout = csv.writer(fout, delimiter="\t")
junk = reader.next() #skip header
csvout.write(["ID", "caseNum", ... ])
for row in reader:
out = row[:2]
out.extend(row[2].split())
out.extend(row[3:])
csvout.write(row)
fout.close()
fin.close()
del csvout #just paranoia, free up structures
del csvin #ditto
-=-=-=-=-
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll 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