Python Forum
getting information from a text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting information from a text file
#1
so i have a text file with api keys and urls.

text file looks like this
api_key = "xxxxxxxxxxxxxxxxxxx"
url = "xxxxxxxxxxxxxxxxxxxx"

is it possible to open the text file and just read what the api_key equals with out reading the whole line?
Reply
#2
You can probably call seek on the file object to move the file pointer to the right place before reading. Why though? What's wrong with reading the line? If you're making your own config file format, you could of course use something that exists already (JSON, YAML, ...).
Reply
#3
split each line on the " delimiter and print 2nd item

import os


def main():
    os.chdir(os.path.abspath(os.path.dirname(__file__)))
    with open('atextfile.txt') as fp:
        for line in fp:
            line = line.strip()
            parts = line.split('"')
            print(parts[1])


if __name__ == '__main__':
    main()
Reply
#4
(Nov-16-2020, 01:39 AM)Nickd12 Wrote: is it possible to open the text file and just read what the api_key equals with out reading the whole line?
How would you know where to start reading and what chunk size to read?

If you can alter the file format - remove the quotes and use configparser module.

Or do as @ndc85430 suggest - use more suitable format.
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
#5
ive tried going the json route still learning how to work with json maybe someone can tell me why im get error

{
"text_to_speech": [
{
"api_key": "xxxxxxxxxxxxxxxxxx",
"api_url": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
],
"speech_to_text": [
{
"api_key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"api_url": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
]
}



with open("ibm_watson_speech.json") as json_file:
    data = json.load(json_file)
    print(data['text_to_speech']['api_key'])
Reply
#6
I don't see any error in your posting. Can you show the complete text of the error or any other output?
Reply
#7
try post 3
Reply
#8
this is the error

print(data['text_to_speech']['api_key'])
TypeError: list indices must be integers or slices, not str

that is the full code
Reply
#9
If we look at your JSON with the structures formatted:
{
  "text_to_speech": 
  [
    {
      "api_key": "xxxxxxxxxxxxxxxxxx",
      "api_url": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    }
  ],
  "speech_to_text": 
  [
    {
      "api_key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      "api_url": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    }
  ]
}
You can more easily see that the api_key element isn't directly inside the "text_to_speech" dictionary, it's inside a list within. So you'd need to either get rid of that enclosing list, or you'd have to access it by asking for the first element in that list:

>>> data["text_to_speech"]["api_key"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not str
>>> data["text_to_speech"][0]["api_key"]
'xxxxxxxxxxxxxxxxxx'
perfringo likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,111 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,649 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 6,949 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,315 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  Saving text file with a click: valueerror i/o operation on closed file vizier87 5 4,390 Nov-16-2020, 07:56 AM
Last Post: Gribouillis
  saving data from text file to CSV file in python having delimiter as space K11 1 2,391 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  Web Form to Python Script to Text File to zip file to web wfsteadman 1 2,130 Aug-09-2020, 02:12 PM
Last Post: snippsat
  Convert Excel file to Text file marvel_plato 6 19,641 Jul-17-2020, 01:45 PM
Last Post: marvel_plato
  Text file information retreval cel 4 2,515 Jun-04-2020, 02:21 AM
Last Post: cel
  Rename file from value in text file Nuge93 1 2,172 Jan-20-2020, 03:50 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

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