2

I was searching for how to define an empty array and fill it using a text file and got stuck at one point and couldn't find the solution.

My Script is :

array=[]
file=open("path\\file.txt","r")
array.append(file.readline())
array.append(file.readline()) #wanted to put only first 2 line just for learning.
incident_count=len(array)
print(incident_count)
print(array)

The first problem is that when I'm trying to put elements in array newline character is also attached("\n"). Also is append right function for putting elements in array.

Second when I'm trying to print the count of array It's printing number of char.

1

3 Answers 3

2

You can use file.readline()[:-1] this will return all the line except the last character which is \n

array.append(file.readline()[:-1])
array.append(file.readline()[:-1])

You can add [:-1] to avoid the last character which is \n consider this example :

hello\n
world\n

so when you read your file line by line the \n is include in each line, so to avoid \n you can read the line from index 0 to index -1

hello\n
^   ^_______index 4 or -1
|___________index 0

to understand more, take a look at this :


2
['hello', 'world']

Or like Omar Einea mention in his comment you can use :

array.append(file.readline().rstrip())
array.append(file.readline().rstrip())

In case the last line not have a \n

Sign up to request clarification or add additional context in comments.

5 Comments

I would go for .rstrip() to avoid removing the last character of the last line (in case last line doesn't have \n)
.rstrip() working as charm.......thnx again.Just a last question the append function I'm using to add elements to array is it the right way to add...?
@ronaldo take a look at my edit how my explanation can help you
@YCF_L thnx man for explaining in such detail......Appreciate it!. can i ask you a last question...? if we are reading hello so it will be from index 0 that is h to index 4 o correct. so how we can put index -1 that is in reverse....
@ronaldo take a look at this stackoverflow.com/questions/509211/… you will understand better ;)
0

You could also use: str.splitlines() method.

You'll end up with something like this:

array.append(file.readline().splitlines())

Comments

0

If your trying to append lines from file to list it always comes with "\n" so to avoid this we need to split the content.

Solution 1:

array=[ ]
with open(rC:\Users\wasim akram\Desktop\was.txt) as fp:
array = fp.read().splitlines()
print(array)

Solution 2 :

myNames=[ ]
f = open(r"C:\Users\wasim akram\Desktop\was.txt",'r')
for line in f:
myNames.append(line.strip())
print(myNames)

9 Comments

thnx for the help.....could you pls tell me is line a reserved keyword (does it have some specific function) or we can write anything here.
The code samples provided need to be indented to work, since python uses spaces to denote blocks of code.
@ronaldo "line" as in the sample above is just a variable.
@phil could you please tell me why we are using a variable and how it's getting just a single line at a time.cause in the above line we are reading the whole text file at once.
@ronaldo check out this question and answers: stackoverflow.com/questions/17949508/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.