Python Forum
Ignore first few letters of a line when reading file.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ignore first few letters of a line when reading file.
#11
@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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#12
I agree.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
Gaming Collection
Homepage
my-python
Reply
#13
(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
Reply
#14
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 "="  
Reply
#15
@buran, so that they can loop through the certain positions and thus letters of the string.
Reply
#16
@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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#17
Very interesting, I never thought of that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question In need of insight regarding Python file reading mechanisms. EnfantNicolas 7 149 Sep-18-2021, 10:39 AM
Last Post: ndc85430
  Help with reading json file hhchenfx 5 538 Jul-07-2021, 01:58 PM
Last Post: hhchenfx
  [Solved] Reading every nth line into a column from txt file Laplace12 7 749 Jun-29-2021, 09:17 AM
Last Post: Laplace12
  help with project of reading and searching big log file korenron 6 373 Jun-24-2021, 01:57 PM
Last Post: korenron
  Reading and storing a line of output from pexpect child eagerissac 0 284 Jun-17-2021, 12:22 PM
Last Post: eagerissac
  Helps with reading csv file - 3 methods hhchenfx 4 646 May-13-2021, 04:15 AM
Last Post: buran
  Subprocess.Popen() not working when reading file path from csv file herwin 13 1,650 May-07-2021, 03:26 PM
Last Post: herwin
Exclamation Why there's a 'blank line' on CSV file? brunolelli 4 843 Mar-25-2021, 03:43 AM
Last Post: buran
  find the header location in a .bin file without reading the whole file at a time SANJIB 0 566 Mar-05-2021, 04:08 PM
Last Post: SANJIB
  Reading a csv file Led_Zeppelin 2 773 Feb-26-2021, 05:48 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020