473,403 Members | 2,293 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,403 software developers and data experts.

Re: File Reading related query

>Is there any function for reading a file while ignoring *\n* occuring in
>the file?

can you be a bit more precise? are we talking about text files or
binary files? how do you want to treat any newlines that actually
appear in the file?
I believe the OP is referencing this behavior:

for line in file('x.txt'):
assert not (
line.endswith('\r') or
line.endswith('\n')), "Don't want this"
else:
yay() # we never end up reaching this

which can be done with a simple generator/wrapper:

def unnewline(iterator):
for line in iterator:
yield line.rstrip('\r\n')
for line in unnewline(file('x.txt')):
assert not (
line.endswith('\r') or
line.endswith('\n')), "Don't want this"
else:
yay() # we get here this time

Alternatively, the content can just be modified on the fly:

for line in file('x.txt'):
line = line.rstrip('\r\n')
...

yes, the interpretation would differ if it were a binary file,
but the above interpretation is a pretty common case (i.e. I
encounter it daily, in my processing of client data-feeds).

-tkc
Sep 17 '08 #1
0 1045

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dan V. | last post by:
Situation: I have to connect with my Windows 2000 server using VS.NET 2003 and C# and connect to a remote Linux server at another company's office and query their XML file. Their file may be...
2
by: cashdeskmac | last post by:
I have a form with 7 datagrids, each on a seperate tabpage and reading from it's own dataTable. I would like to take the first three fields of each row and show them in a listbox (for example, ID,...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
6
by: Ian | last post by:
Hi I'm pretty new at this so please don't laugh too hard. I'm trying to load an xml document using VB.NET and having a hard time. My code doesn't crash but it doesn't work either, the first...
2
by: Andrew Clark | last post by:
Hello, I am 99% sure that the problem I am having is not PHP related, but to remove my doubt, I am posting here. I have a search page (interfacing with MySql) that displays results in an...
1
by: Chandra | last post by:
Hi, I have a problem related to File Reading. I have a file which contains data which is in Hexa format. I have to read that data and push it to a function which takes a parameter of type char....
2
by: Zach | last post by:
I compiled a game client and it crashed (segmentation fault) resulting in a core file being generated. I'm trying to find out exactly what caused it to crash. Any ideas how I can do this with gdb?...
1
by: stevedub | last post by:
I am having some trouble configuring my array to read from a sequential file, and then calling on that to fill an array of interests. I think I have the class set up to read the file, but when I run...
1
by: Fredrik Lundh | last post by:
Usman Ajmal wrote: can you be a bit more precise? are we talking about text files or binary files? how do you want to treat any newlines that actually appear in the file? </F>
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
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.