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.
#1
Hello, everybody!

I have run into a spot of bother while writing my Python script and I am wondering if anyone can help me out.

I am trying to read a text file using Python. That goes well and I am able to read the file line-by-line (using the File_Object.readline() function), which is my goal. However, to make my file more organized, I do this, for example my first line:

Quote: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".

Is there anyway I am able to do this? I also want to add that the variable after "name = " is randomised and may not always be "John". I just need to ignore the "name = ". Thank you.
Reply
#2
Show what you have so far. Without seeing your code, we can't help.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
Gaming Collection
Homepage
my-python
Reply
#3
show your code, as well as sample input file
it may turn you can use configparser instead of parsing the file yourself (you can alsway split at '=')
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
#4
(May-29-2020, 11:25 AM)buran Wrote: show your code, as well as sample input file
it may turn you can use configparser instead of parsing the file yourself (you can alsway split at '=')
How am I able to split at "="?
Reply
#5
>>> spam = 'name=John'
>>> spam
'name=John'
>>> spam.split('=')
['name', 'John']
>>> spam.split('=')[1]
'John'
>>> eggs = spam.split('=')[1]
>>> eggs
'John'
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
#6
Untested example
text = 'name = mytext'
newtext = text.split('=')
print(newtext[1])
Output:
mytext
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
Gaming Collection
Homepage
my-python
Reply
#7
(May-29-2020, 12:47 PM)buran Wrote:
>>> spam = 'name=John'
>>> spam
'name=John'
>>> spam.split('=')
['name', 'John']
>>> spam.split('=')[1]
'John'
>>> eggs = spam.split('=')[1]
>>> eggs
'John'

Thank you very much, it functions perfectly, but I have one question:

I want to make this string a raw string:

line2 = (f.split('= ')[1])
I believe that when creating a raw string, you place an R before the string, like this:
raw_string = R"This is a raw string."
However, because my string doesn't contain any ", the R does not work. Is there any way I could make a raw string with what I have?

Thank you again.

EDIT: In fact, how can I ignore \n at the end of a line?
Reply
#8
the answer to your question depends on what you are trying to get at.

1) If the user inputs something and you need to split it, or if you just need to split a certain string.

You can use split() for the first possibility. Or you can break up your string with list()
example: string = "string", string_list = list(string) and then use a for loop to loop through the needed letters or numbers.
Reply
#9
you are feeding us info with a spoon. describe the broad picture so that we can help you in best possible way.
obviously you reading this from a file and as I said there may be better way to deal with it.
as to removing new line at the end - one way is to use str.strip() method (similiar to str.split())
as to raw string - raw strings are not a different kind of string. They are a different way of describing a string in your source code. i.e. raw strings apply only to string literals. they exist so that you can more conveniently express strings that would be modified by escape sequence processing
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
#10
As stated above, depends on what you're wanting
From previous example
raw = 'name = R"my string"'
newtxt = raw.split('=')
print(newtxt[1])
Output:
R"my string"
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
Gaming Collection
Homepage
my-python
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with reading json file hhchenfx 5 294 Jul-07-2021, 01:58 PM
Last Post: hhchenfx
  [Solved] Reading every nth line into a column from txt file Laplace12 7 480 Jun-29-2021, 09:17 AM
Last Post: Laplace12
  help with project of reading and searching big log file korenron 6 269 Jun-24-2021, 01:57 PM
Last Post: korenron
  Reading and storing a line of output from pexpect child eagerissac 0 174 Jun-17-2021, 12:22 PM
Last Post: eagerissac
  Helps with reading csv file - 3 methods hhchenfx 4 487 May-13-2021, 04:15 AM
Last Post: buran
  Subprocess.Popen() not working when reading file path from csv file herwin 13 1,146 May-07-2021, 03:26 PM
Last Post: herwin
Exclamation Why there's a 'blank line' on CSV file? brunolelli 4 693 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 479 Mar-05-2021, 04:08 PM
Last Post: SANJIB
  Reading a csv file Led_Zeppelin 2 651 Feb-26-2021, 05:48 AM
Last Post: buran
  reading a csv file Led_Zeppelin 3 845 Feb-19-2021, 02:16 PM
Last Post: Led_Zeppelin

Forum Jump:

User Panel Messages

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