Python Forum
Ignore first few letters of a line when reading file. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Ignore first few letters of a line when reading file. (/thread-27199.html)

Pages: 1 2


RE: Ignore first few letters of a line when reading file. - buran - May-29-2020

@BitPythoner, why would they want to convert their str to list?
@menator01, what you show is actually just str with first char being R, it's not raw string. Not that OP request makes sense too


RE: Ignore first few letters of a line when reading file. - menator01 - May-29-2020

I agree.


RE: Ignore first few letters of a line when reading file. - Yoriz - May-29-2020

(May-29-2020, 11:14 AM)ShakeyPakey Wrote: Hello, everybody!

"name = John"

I want to use "John" only as a variable, and I don't want to read the first 7 bytes of the line: "name = "
I only want to read "John".
line = 'name = John'
print(line[7:])
Output:
John



RE: Ignore first few letters of a line when reading file. - ShakeyPakey - May-29-2020

The issue is solved. Thanks for everyone who has been part of this thread.

Here is the solution:
with open("file.txt",) as f:
    line = f.readline() # Line is read from file. In this example, the line is "name = John".
    name = line.split() 
    print("My name is" + name[2]) # This should print out "My name is John". John is the third string in the line, 
    # after "name" and "="  



RE: Ignore first few letters of a line when reading file. - BitPythoner - May-30-2020

@buran, so that they can loop through the certain positions and thus letters of the string.


RE: Ignore first few letters of a line when reading file. - buran - May-30-2020

@BitPythoner, in python you don't need the position, nor need to convert to list - you can iterate over str:
>>> spam = 'name = John'
>>> for char in spam:
...     print(char)
... 
n
a
m
e
 
=
 
J
o
h
n
>>> for idx, char in enumerate(spam):
...     print(f'position:{idx}, char:{char}')
... 
position:0, char:n
position:1, char:a
position:2, char:m
position:3, char:e
position:4, char: 
position:5, char:=
position:6, char: 
position:7, char:J
position:8, char:o
position:9, char:h
position:10, char:n



RE: Ignore first few letters of a line when reading file. - BitPythoner - May-30-2020

Very interesting, I never thought of that.