Python readline() is not reading a line with single space

2024/10/9 2:22:56

I am reading a text file using readline(). My file contains below content with a space at second line:

!"
#
$
%
& 

When I print read values using-

print("'%s'\n" % iso5_char)

It prints-

'!''''"''#''$''%''&'

It seems that readline() not reading the 'space' at second line.

I am using python for the first time. I have python-3.5 installed. What is wrong I am doing here? Why is space not read?

UPDATE: Screenshot of the file i amreading:

enter image description here

Answer

Based on a comment to the original question, you are explicitly stripping out the space with this expression:

iso5_char.rstrip()

From the documentation for rstrip:

Return a copy of the string with trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the end of the string this method is called on.

In this context, "whitespace" refers to spaces, tabs, newlines, carriage returns, formfeeds, and vertical tabs.

If your intent with rstrip is to only strip trailing newlines, you can pass a newline to the rstrip command:

iso5_char.rstrip('\n')

Or, you can simply chop off the last character since it's safe to assume it's a newline:

iso5_char[:-1]
https://en.xdnf.cn/q/118642.html

Related Q&A

Bisection search [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Using bisection search to determine I have posted other thread but it did not receive answers thus im trying to provide so…

What am i doing wrong with matplotlibs yticks?

My code is as follows (dont ask for the variable names, im german^^):import matplotlib.pyplot as plt import numpy as np strecke = [] zeit = []daten = open("BewegungBeschleunigung.csv")for i i…

Python- How to implement quick sort when middle element is the pivot?

There are many different versions of quickSort that pick pivot in different ways.Always pick the first element or the last element as the pivot Pick a random element as a pivot. Pick median as the pivo…

How to expose a form when a radio button is checked?

Consider the following sample html template,<html><body><input type="radio" name="x">x</input><br><input type="radio" name="y"&g…

How to generate perlin noise in pygame?

I am trying to make a survival game and I have a problem with perlin noise. My program gives me this:But I want something like islands or rivers. Heres my code: #SetUp# import pygame, sys, random pygam…

Inputting range of ports with nmap optparser

This is the scriptimport nmap import optparsedef nmapScan(tgtHost,tgtPort):nmScan = nmap.PortScanner()nmScan.scan(tgtHost,tgtPort)state=nmScan[tgtHost][tcp][int(tgtPort)][state]print "[*] " +…

Implementing a Python algorithm for solving the n-queens problem efficiently

I am working on a project that requires me to solve the n-queens problem efficiently using Python. I have already implemented a basic recursive algorithm to generate all possible solutions, but I am lo…

Annotations with pointplot

I am using a pointplot in seaborn.import seaborn as sns sns.set_style("darkgrid") tips = sns.load_dataset("tips") ax = sns.pointplot(x="time", y="total_bill", hu…

How do I get data in tuples and tuples in lists?

I am trying to figure out the route that a car takes in a fictional manhattan. I have defined the starting position, being(1,2)(in a 2 dimensional grid).manhattan=[[1, 2, 3, 4, 5],[6, 7, 8, 9, 10],[11…

How to return a value from a table or dataframe given 2 inputs? Python

Lets say this is my dataframe:and the user inputs B and 2Then the function would return clemintineIs there a way to do this without using a bunch of if elif statements. The actual dataframe Im working …