Advertisement

03.18.2008 at 02:32AM PDT, ID: 23249583 | Points: 250
[x]
Attachment Details

how to convert a comma delimited file to tab delimited file using python

Tags: python
I have a csv output from a python file and I want to convert it to a tab delimted file. The file is too big to ooen in Excel. I think I need to use a dialect class for the CSV module but I am not sure. The top row of the file contains the column names and the current file name is testData.csv.
Thanks for any help
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: onyourmark
Question Asked On: 03.18.2008
Participating Experts: 2
Points: 250
Views: 0
Translate:
Loading Advertisement...
03.18.2008 at 02:45AM PDT, ID: 21149428

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.18.2008 at 02:58AM PDT, ID: 21149463

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.18.2008 at 09:31AM PDT, ID: 21152838

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.19.2008 at 01:19PM PDT, ID: 21165467

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.20.2008 at 04:32AM PDT, ID: 21170084

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.20.2008 at 05:04AM PDT, ID: 21170325

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.20.2008 at 06:04AM PDT, ID: 21170852

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.20.2008 at 06:13AM PDT, ID: 21170954

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
03.18.2008 at 02:45AM PDT, ID: 21149428
 
03.18.2008 at 02:58AM PDT, ID: 21149463
I was trying to do it in python but thank you anyway.
 
03.18.2008 at 09:31AM PDT, ID: 21152838
Hi, is the answer as simple as
for line in open("testdatasent.csv"):
line.replace (',', '      ')
 
03.19.2008 at 01:19PM PDT, ID: 21165467

Rank: Guru

> for line in open("testdatasent.csv"):
> line.replace (',', '      ')

If the file were reasonably small then the code could be enhanced to

f = open('testdatasent.csv')
content = f.read()
new_content = content.replace(',', '\t')
f.close()

and the new_content could be used directly or could be written to another file. However, data stored in csv files are not that simple. Generally, the csv format must be capable to capture also the string values with commas inside. The strings are quoted for that purpose. The comma inside a string is not the separator. Parser of the csv files is therefore not as easy as splitting on commas.

You should have a look at the Python standard module csv (see http://docs.python.org/lib/module-csv.html). See also some examples how to use it -- http://docs.python.org/lib/csv-examples.html
 
03.20.2008 at 04:32AM PDT, ID: 21170084
Hi. I did look at the csv module but could not figure out how to output to tab delimited.
 
03.20.2008 at 05:04AM PDT, ID: 21170325
Would this work?
f = open('myTest2.csv')
content = f.read()
new_content = content.replace(',', '\t')
f.close()
text_file=open("myTest.txt", "w")
text_file.write(new_content)
text_file.close()
 
03.20.2008 at 06:04AM PDT, ID: 21170852

Rank: Guru

As said above, this would not work for csv files with commas inside strings. Otherwise, this would be fine.

But you can try the following. Save the next lines into some.csv

===================================
1,"string",2.58
2,"string B, with comma inside",15
===================================

And then save the snippet into the testCSV.py (or so)... This will generate output.txt with tab delimited values.

The principle is based on the fact that a csv.reader() class instance returns rows that take the form of lists of values (try to uncomment the print row command to visualize it on display). The list of values can be connected to a string using the join() method of the built-in string class. The separator is the string for which the join() is called. This way the command

'\t'.join(row)

joins all values using the tabulator into a single string. The \n is appended after and the resulting string is written to the output text file.

Possibly another way could be to use the csv.writer() instance with dialect='excel-tab'. I did not try, but I expect the same result.


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
import csv
 
fin = open('some.csv', 'rb')  # doc says that binary mode must be used
fout = open('output.txt', 'w')
 
reader = csv.reader(fin)
 
for row in reader:
   ##print row
   fout.write('\t'.join(row) + '\n')
 
fin.close()
fout.close()
Open in New Window
 
03.20.2008 at 06:13AM PDT, ID: 21170954

Rank: Guru

I tried it with the writer. The output file also must be opened in binary mode. See the snippet.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
import csv
 
fin = open('some.csv', 'rb')     # doc says that binary mode must be used
fout = open('output.txt', 'wb')  # output also have to be in binary mode
 
reader = csv.reader(fin)
writer = csv.writer(fout, dialect='excel-tab')
 
for row in reader:
   writer.writerow(row)
 
fin.close()
fout.close()
Open in New Window
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628