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


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

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.


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

Show what you have so far. Without seeing your code, we can't help.


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

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 '=')


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

(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 "="?


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

>>> spam = 'name=John'
>>> spam
'name=John'
>>> spam.split('=')
['name', 'John']
>>> spam.split('=')[1]
'John'
>>> eggs = spam.split('=')[1]
>>> eggs
'John'



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

Untested example
text = 'name = mytext'
newtext = text.split('=')
print(newtext[1])
Output:
mytext



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

(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?


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

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.


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

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


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

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"