471,889 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,889 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:

"joe blow",21,salesman,married
"hanna montana,19,entertainer,single


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?
Dec 12 '08 #1
1 1884
2,851 Expert Mod 2GB
Please use code tags when posting code!!

Python for loops and if statements require a colon for proper syntax.
Expand|Select|Wrap|Line Numbers
  1. for name in nameList:
  2.     print name
  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.
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')
  4. results = []
  5. for line in infile:
  6.     results.append([item for item in line.strip().split(',')[::-1]])
  7. infile.close()
  9. outfile.write('\n'.join([','.join(item) for item in results]))
  10. outfile.close()
  12. ''' output file contents
  13. status,occupation,age,name
  14. married,salesman,21,joe blow
  15. single,entertainer,19,hanna montana
  16. '''
  18. infile = open('names.txt')
  19. outfile = open('names_reversed1.txt', 'w')
  21. results = []
  22. for line in infile:
  23.     lineList = line.strip().split(',')
  24.     lineList = lineList[::-1]
  25.     results.append(lineList)
  26. infile.close()
  28. output = []
  29. for item in results:
  30.     output.append(','.join(item))
  32. outfile.write('\n'.join(output))
  33. outfile.close()
Dec 12 '08 #2

Post your reply

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

Similar topics

1 post views Thread by The Bear | last post: by
3 posts views Thread by Joe Fisherman | last post: by
1 post views Thread by Fordraiders | last post: by
4 posts views Thread by Ron | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by zermasroor | last post: by

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.