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.
My Github
How to post code using bbtags


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.
My Github
How to post code using bbtags


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.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading and storing a line of output from pexpect child eagerissac 1 4,218 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  Copy Paste excel files based on the first letters of the file name Viento 2 419 Feb-07-2024, 12:24 PM
Last Post: Viento
Sad problems with reading csv file. MassiJames 3 618 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Reading in of line not working? garynewport 2 831 Sep-19-2023, 02:22 PM
Last Post: snippsat
  Reading a file name fron a folder on my desktop Fiona 4 894 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,088 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,536 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Getting last line of each line occurrence in a file tester_V 1 856 Jan-31-2023, 09:29 PM
Last Post: deanhystad
  Reading a file JonWayn 3 1,090 Dec-30-2022, 10:18 AM
Last Post: ibreeden
  Reading Specific Rows In a CSV File finndude 3 972 Dec-13-2022, 03:19 PM
Last Post: finndude

Forum Jump:

User Panel Messages

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