求助一个Python 循环读取文件,并对读到的字符串进行赋值,然后进一步生成json file 的方法。
本来想用shell写,利用while read line do ......done <table.list这样的。
但是这样json file 不知道怎么写了,所以只能试着用Python写个循环。
目前的问题是TABLE.csv文件中TABLE是个变量,是从table.list里面取到的表名,不知道Python 里面变量怎么用。求助大神!!
import base64
import os
with open('table.list','r') as f4:
for line in f4:
TABLE = line
f = open('TABLE.csv', 'r')
content = f.read()
content1 = content.encode(encoding='utf-8')
content2 = base64.b64encode(content1)
f.close()
if os.path.getsize('TABLE.step1'):
print('file exists and not empty')
f1= open('TABLE.step2','r')
V_Sha=f1.read()
f1.close()
with open('TABLE.csv.json', 'w') as j1:
j1.write('{ "message": "[ci skip][Auto Commit]data into file", "content": "' + content2 + '", "sha": "' + V_Sha + '", "branch": "xxx" }')
else:
with open('TABLE.csv.json', 'w') as j1:
j1.write('{ "message": "[ci skip][Auto Commit]data into file", "content": "' + content2 + '", "branch": "xxx" }')
f4.close()