f = open('database.txt','r')
data_set = ""
while 1:
line = f.readline()
if not line:break
data_set += line
print(data_set)
print()
I need to change the Title instances in the database to title case
代码到目前为止,我将在评论中提供列表,因为我在添加到这里时遇到了麻烦
file = open("database.txt", 'r')
lines = file.readlines()
data_set = [line.split(' , ') for line in open ("database.txt")]
print (data_set)
for i in range(len(data_set[0])):
data_set[0][i]=s[i].t()
print(data_set)
将数据库转换为上述代码中的列表:[['ID = j234hg \ n'],['日期= 1969年10月19日\ n'],['Title =宫廷场景与红衣主教richelieu \ n'] ,['ID = d45j5jkd \ n'],['日期= 1969年12月28日\ n'],['标题=皇家飞利浦ORCHESTRA前往浴室\ n'],['ID = s4k5jk \ n'], ['日期= 1970年12月8日\ n'],['标题=在三轮车上穿越大西洋\ n'],['ID = zd7u4h \ n'],['日期= 1969年10月19日\ n'],[ 'Title = Bicycle Repair Man \ n'],['ID = f983 \ n'],['Date = 1970年12月22日\ n'],['Title = Royal Episode 13(或:The Queen Will Be Watching)\ n'],['ID = j8s74 \ n'],['日期= 1970年9月15日\ n'],['标题=令人惊叹的高度\ n'的SEMAPHORE版本],['ID = n4j6l3j \ n'] ,['日期= 1972年12月7日\ n'],['标题=先生。 Pither']]
答案 0 :(得分:0)
遍历data_set并在'Title ='之后获取文本并使用str.title()!
data_set = [['ID=j234hg\n'], ['Date=19 October 1969\n'], ['Title=court scene with cardinal richelieu\n'], ['ID=d45j5jkd\n'], ['Date=28 December 1969\n'], ['Title=THE ROYAL PHILHARMONIC ORCHESTRA GOES TO THE BATHROOM\n'], ['ID=s4k5jk\n'], ['Date=8 December 1970\n'], ['Title=crossing the atlantic on a tricycle\n'], ['ID=zd7u4h\n'], ['Date=19 October 1969\n'], ['Title=Bicycle Repair Man\n'], ['ID=f983\n'], ['Date=22 December 1970\n'], ['Title=Royal Episode 13 (or: The Queen Will Be Watching)\n'], ['ID=j8s74\n'], ['Date=15 September 1970\n'], ['Title=THE SEMAPHORE VERSION OF WUTHERING HEIGHTS\n'], ['ID=n4j6l3j\n'], ['Date=7 December 1972\n'], ['Title=Mr. Pither']]
for index,l in enumerate(data_set):
column_name,value = l[0].split("=")
if 'Title' == column_name:
data_set[index]="=".join([column_name+value.title()])
print data_set
答案 1 :(得分:0)
data_set = [['ID=j234hg\n'], ['Date=19 October 1969\n'], ['Title=court scene with cardinal richelieu\n'], ['ID=d45j5jkd\n'], ['Date=28 December 1969\n'], ['Title=THE ROYAL PHILHARMONIC ORCHESTRA GOES TO THE BATHROOM\n'], ['ID=s4k5jk\n'], ['Date=8 December 1970\n'], ['Title=crossing the atlantic on a tricycle\n'], ['ID=zd7u4h\n'], ['Date=19 October 1969\n'], ['Title=Bicycle Repair Man\n'], ['ID=f983\n'], ['Date=22 December 1970\n'], ['Title=Royal Episode 13 (or: The Queen Will Be Watching)\n'], ['ID=j8s74\n'], ['Date=15 September 1970\n'], ['Title=THE SEMAPHORE VERSION OF WUTHERING HEIGHTS\n'], ['ID=n4j6l3j\n'], ['Date=7 December 1972\n'], ['Title=Mr. Pither']]
for index,l in enumerate(data_set):
if 'Title=' == l[0][:6]:
data_set[index]="Title="+l[0][6:].title()
print data_set