473,320 Members | 1,926 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Noobie Question reg Delimited Text

Hope the Community can bear with me as I muddle with the vocabulary since I am not really sure if I am going about this the correct way.

My question is as follows:

If I had some sample data in the form of delimited text, in this case comma-delimited (eg. canned_data.txt) and the format looked roughly like this:

name,age,occupation,status
"joe blow",21,salesman,married
"hanna montana,19,entertainer,single

etc...


I am confused with how to go about

1) importing this file into Python for reading line-by-line for eventual parsing,splitting, general manipulation to a new output file
2) the exact terminology/vocab (I have come across array,matrix,table, etc.)


So far I would do something like this:
Expand|Select|Wrap|Line Numbers
  1. # start of my module
  2. tInputName = raw_input("Enter the name of your delimited input file:") #canned_data.txt in the same directory as module
  3. tOutputName= raw_input("Enter the name of your output file:")
  4. getData = open(tInputName,r)
  5. getData = input.readlines()
  6. for line in getData
  7.    getData = line.split(",") #my series of general data manipulation
  8.    getData = line.upper #another eg of manipulation
  9. output.writelines(tOutputName)
  10. output.close()
  11. if output.close == true
  12. print "The file:", tOutputName, " was created"
  13. raw_input()
  14. else
  15. output.close ==false
  16. print "Failed creating  ", tOutputName
  17. raw_input()

In a nutshell, could someone help me vett the above?
Sincerely,
Nick
Dec 12 '08 #1
1 1948
bvdet
2,851 Expert Mod 2GB
Please use code tags when posting code!!

Python for loops and if statements require a colon for proper syntax.
Example:
Expand|Select|Wrap|Line Numbers
  1. for name in nameList:
  2.     print name
  3.  
  4. if name == "Fred":
  5.     print "Fred's here."
  6. else:
  7.     print "I don't know this guy."
Generally, if an error occurs when opening, reading, or writing to a file, an IOError exception is raised. This error can be caught with a try/except block.
Example:
Expand|Select|Wrap|Line Numbers
  1. >>> try:
  2. ...     f = open('abc')
  3. ... except IOError, e:
  4. ...     print e
  5. ...     print e.errno, e.strerror, e.filename
  6. ...     
  7. [Errno 2] No such file or directory: 'abc'
  8. 2 No such file or directory abc
  9. >>> 
File I/O seldom errors except when invalid file names or paths are attempted.

Following are examples of opening a file, manipulating the data, and writing to another file. The first example uses list comprehensions. The second is the equivalent without the list comprehensions.
Expand|Select|Wrap|Line Numbers
  1. infile = open('names.txt')
  2. outfile = open('names_reversed.txt', 'w')
  3.  
  4. results = []
  5. for line in infile:
  6.     results.append([item for item in line.strip().split(',')[::-1]])
  7. infile.close()
  8.  
  9. outfile.write('\n'.join([','.join(item) for item in results]))
  10. outfile.close()
  11.  
  12. ''' output file contents
  13. status,occupation,age,name
  14. married,salesman,21,joe blow
  15. single,entertainer,19,hanna montana
  16. '''
  17.  
  18. infile = open('names.txt')
  19. outfile = open('names_reversed1.txt', 'w')
  20.  
  21. results = []
  22. for line in infile:
  23.     lineList = line.strip().split(',')
  24.     lineList = lineList[::-1]
  25.     results.append(lineList)
  26. infile.close()
  27.  
  28. output = []
  29. for item in results:
  30.     output.append(','.join(item))
  31.  
  32. outfile.write('\n'.join(output))
  33. outfile.close()
Dec 12 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Christine Forber | last post by:
I wonder if anyone knows of some javascript code to check a comma-delimited list of email addresses for basic formating. What I'm looking for is the javascript code to check a form field on form...
3
by: Ben | last post by:
Hi all - I am having a bit of trouble and thought maybe someone in this group could shed some light. Here's the skinny... I am creating an automated process to import a bunch of text files...
1
by: The Bear | last post by:
I have populated a dataset with a tab delimited text file. When the changes are made in the dataset, I then want to write those changes from the dataset to a tab delimited text file. How do I write...
3
by: Joe Fisherman | last post by:
I have used regex to parse a huge text file, and grab a tab delimited portion of it. I often use comma delimited text files, and use Jet Oledb4. I read that I would need an ini if the file wasn't...
2
by: denisel | last post by:
Hi, We will be conducting surveys through SurveyMonkey online and will be importing the answers by tab delimited or comma delimited file into access. I was wondering if there is specific way to...
1
by: Fordraiders | last post by:
vb.net 2003 Office 2003 What I have: C:\TestData\Input.txt Text File Pipe Delimited : 4 columns Of data example: 00001|NO BRAND NAME ASSIGNED|6DU27|M3-.5 X 6 FLAT HD SOCKET CAP SCREW,...
4
by: Ron | last post by:
How do I display delimited text on multiple lines in a listbox? For example in my textbox I have this: Joe Doe,123 Street,Mytown and In a listbox then I want to display: Joe Doe 123 Street...
2
by: Ron | last post by:
so if my textbox is named textbox1 and my listbox is named ltsdisplay, for the button that would make this all happen I would just need to: ...
6
by: =?Utf-8?B?UmljaA==?= | last post by:
'--this code works but only reads text into one column when contains multiple cols Dim ds1x As New DataSet Dim ConStr As String = _ "Provider=Microsoft.Jet.OLEDB.4.0;Data...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.