以下のようなソースを入力すると、エラーが出てしまいます。最初はうまくいったのですが、その後、エラーメッセージが出て、どうしてもうまくいきません。
やりたいことは、Chap01リストから、タイトル名を持ってきて、それを表示させたいです。conf.CHAP01_TITLE[filename]とすると、問題なく動きます。dir_title[filename]だとエラーが出ます。
よろしくお願いします。
python
1(Chap01/index.py) 2#!/usr/bin/python 3# -*- coding: utf-8 -*- 4 5#configファイルをimportできるようにパスを通す 6import sys,os 7sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../') 8 9import config as conf 10 11filename = os.path.basename(__file__).strip('.py') #自身のファイル名取得 12 13dir_title = 'conf.' + os.path.dirname(__file__)[-6:].upper() +'_TITLE' #自身のディレクトリ名取得で大文字変換(conf.CHAP**_TITLE) 14 15 16print (""" 17<!DOCTYPE html> 18<html lan="ja"> 19<head> 20 {1} 21 <title>{4}-{0}-</title> 22 {2} 23 {3} 24</head>""").format(conf.MAIN_TITLE, conf.METATAG, conf.FONTAWESOME, conf.STYLE_LINK, dir_title[filename])
python
1(config.py) 2HAP01_TITLE = {'index':'1章「プログラミングって何だ?」', '01':'1.hoge', '02':'2.hogege', '03':'3.hogegege', '04':'4.hogegegege'} #1章タイトル群 3
(エラーメッセージ) File "~/cgi-bin/Chap01/index.py", line 23, in <module> </head>""").format(conf.MAIN_TITLE, conf.METATAG, conf.FONTAWESOME, conf.STYLE_LINK, dir_title[filename]) TypeError: string indices must be integers, not str
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/03/18 23:23
2018/03/18 23:30
2018/03/19 03:35