473,414 Members | 1,631 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,414 software developers and data experts.

Reading a file and splitting at the same time

Is there a way to read and split it at the same time?
I currently use this code.
Expand|Select|Wrap|Line Numbers
  1. chooseFile = raw_input ("Type File Name:")
  2. outputFile = raw_input ("Type File Output name with extension:")
  3.  
  4. myfile=open(chooseFile,'r')
  5. myList =[]
  6. myList = myfile.readlines()
  7.  
  8. splitList = []
  9.  
  10. for i in myList:
  11.     splitList.append (i.split(';'))
  12.  
It basically reads in a semicolon delimited file and splits it into a list.
But I think there may be a more elegant way to do this.
Apr 25 '13 #1

✓ answered by bvdet

You do not need to initialize the empty list "myList". If you create an open file object by assignment to an identifier, you should close it when you are done:myfile.close()
"splitList" can be created using a list comprehension:
Expand|Select|Wrap|Line Numbers
  1. splitList = [line.split(";") for line in open(chooseFile).readlines()]

1 1638
bvdet
2,851 Expert Mod 2GB
You do not need to initialize the empty list "myList". If you create an open file object by assignment to an identifier, you should close it when you are done:myfile.close()
"splitList" can be created using a list comprehension:
Expand|Select|Wrap|Line Numbers
  1. splitList = [line.split(";") for line in open(chooseFile).readlines()]
Apr 25 '13 #2

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

Similar topics

3
by: SK | last post by:
I have a file. i get the creation time using File.GetCreationTime. then i go and delete that file. and then create it again and print the File.GetCreationTime. It is giving me the old creation...
1
by: Pierre Maricq | last post by:
Hi, I am using Win2000 and Access2000. I need to build build a macro or write a VBA in Access that would screen all files contained in a directory on my C drive (files are structrured DAT...
2
by: novacreatura | last post by:
Hi, I have a project that's supposed to create a program for a "Dating Service". The first part of the program is to read a textfile of profiles which include names, age, etc...into a string...
1
by: zahidkhan | last post by:
Hi All, Any body has any idea abt this. I have to write a function which will take existing file name as an input parmeter and will return file creation time. I need to implement this function...
3
by: imrantbd | last post by:
Hi this is Imran, I am facing some problem to track file upload time in a linux hosting. I used filectime / filemtime to track date. But I get the time stamp of locally file creation time.I can...
3
by: miss time | last post by:
Hi all, my java friends ^-^ I have next week quiz in reading file text ,and understand the topic very well. can any one give some question related to this topic .this help me more to...
0
by: Edwin.Madari | last post by:
os.paht.gmtime(path) returns the last modification of path. check out http://docs.python.org/lib/module-os.path.html regards Edwin -----Original Message----- From:...
1
by: ndedhia1 | last post by:
I was reading in a log file like this that had no milliseconds: QuoteBlockTiming exceeded 1 ms: 1 --- Thu Dec 10 02:01:40 CST 2009 170.137.15.155 Class key = 601650761 block size QuoteBlockTiming...
1
Coldfire
by: Coldfire | last post by:
Hi, The strange problem i am having is, the input element of type='file' not reading file names after 20 file elements. It simple returns null on reading the 'name' of file. The code is...
1
by: bjoarn | last post by:
I have an Application C# handling file reading, building index on this file, using dll wrapped with SWIG. The dll is originaly programmed in C++. Dll reports back to the the C# programm throug...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.