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.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
ronaldo
  • 107
  • 1
  • 2
  • 10
  • Possible duplicate of [Reading a file without newlines](https://stackoverflow.com/questions/12330522/reading-a-file-without-newlines) – Oluwafemi Sule Feb 24 '18 at 06:51

3 Answers3

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

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • 1
    I would go for `.rstrip()` to avoid removing the last character of the last line (in case last line doesn't have `\n`) – Omar Einea Feb 24 '18 at 06:53
  • .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 Feb 24 '18 at 07:01
  • @ronaldo take a look at my edit how my explanation can help you – Youcef LAIDANI Feb 24 '18 at 07:01
  • @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 Feb 24 '18 at 07:09
  • @ronaldo take a look at this https://stackoverflow.com/questions/509211/understanding-pythons-slice-notation you will understand better ;) – Youcef LAIDANI Feb 24 '18 at 07:10
0

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

You'll end up with something like this:

array.append(file.readline().splitlines())
Carlos Parra
  • 957
  • 9
  • 19
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)

  • 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. – ronaldo Feb 24 '18 at 07:16
  • The code samples provided need to be indented to work, since python uses spaces to denote blocks of code. – Phil Feb 24 '18 at 07:17
  • @ronaldo "line" as in the sample above is just a variable. – Phil Feb 24 '18 at 07:18
  • @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 Feb 24 '18 at 07:19
  • @ronaldo check out this question and answers: https://stackoverflow.com/questions/17949508/python-read-all-text-file-lines-in-loop – Phil Feb 24 '18 at 07:25
  • @ronaldo In python if you iterate then line (just variable) will be list of all lines present in a file. You can change line to some other name also it will work – sk Sareen Feb 24 '18 at 07:31
  • @Phil thnx for the post.I read the post and checked on internet also but pls can you help for clearing my doubt as I'm still getting that if we have to read line by line we use readline() function but here we have only opened the file and in the for loop also not used any function to get a single line only have used the strip function to get rid od whitelines.So, how a single line is getting fetch instead of whole file – ronaldo Feb 24 '18 at 07:32
  • @waseem akram shaik ohhhh thnx a lot now I'm getting it. – ronaldo Feb 24 '18 at 07:33
  • @ronaldo readline() is not the only way to read lines. The loop construct is iterating over a thing which is iterable. – Phil Feb 24 '18 at 07:36