在Python中替换列表中的单引号

时间:2018-06-27 12:34:02

标签: python python-3.x google-cloud-platform

我正在查询bigquery以获取其架构,该架构将返回以下列表

['word STRING', 'word_count INTEGER', 'corpus STRING', 'corpus_date INTEGER']

在此输出列表中,我尝试使用下面的代码将单引号替换为空,将INTEGER替换为BIGINT

# Output from the query    
result = ['word STRING', 'word_count INTEGER', 'corpus STRING', 'corpus_date INTEGER']
result_new = [string.replace("INTEGER", "BIGINT").replace("'", "") for string in result]
result_new = 'create table {} {} stored as orc;'.format(table_id, result_new)
print(result_new) 

其返回结果为:

create table shakespeare ['word STRING', 'word_count BIGINT', 'corpus STRING', 'corpus_date BIGINT'] stored as orc;

我想要的输出是:

create table Shakespeare (word STRING, word_count BIGINT, corpus STRING, corpus_date BIGINT) stored as orc;

由于我是Python的全新用户,因此我在Google上进行了很多尝试,但尝试将INTEGER替换为BIGINT,但无法替换其他内容。

有什么方便的方法可以做到这一点吗?

1 个答案:

答案 0 :(得分:3)

在您的示例中,您使用的是列表表示形式,其中包括方括号和引号,引号不在字符串本身中,而是在列表的表示形式中,当您将Python包含在另一个带有{ {1}}。

更好的方法是按照您想要的方式直接构建字符串:

.format