python添加双引号和逗号(掌握)

####代码如下:
# 导入需要使用的模块
import os
import tkinter.filedialog
fn = tkinter.filedialog.askopenfilename(title='选择了一个文件',filetypes=[('文本文件','.txt'),('所有文件','.*')])
print("打开了文件:"+fn)


line_count=0
f = open(fn,'r',encoding='utf8',errors='ignore')

result = list()
for line in f.readlines():
line = line.strip()
if not len(line) or line.startswith('#'):
continue
line = '"'+ line+'",'
result.append(line)
line_count += 1
print("结果是%s"%result)


return_str = "\n"
open('C:/Users/admin/Desktop/test.txt','w').write('%s'% return_str.join(result))
print("处理了文件:" +fn+"中的"+str(line_count)+"行非空白行")

##实现结果

 ##################python添加txt文本左半边相同的部分:代码如下:

# 导入需要使用的模块
import os
import tkinter.filedialog
fn = tkinter.filedialog.askopenfilename(title='选择了一个文件',filetypes=[('文本文件','.txt'),('所有文件','.*')])
print("打开了文件:"+fn)


line_count=0
f = open(fn,'r',encoding='utf8',errors='ignore')

result = list()
for line in f.readlines():
line = line.strip()
if not len(line) or line.startswith('#'):
continue
line = 'INSERT INTO broadband_region_organization_info(province, city, country, organization_code, organization_name, `type`, create_time, update_time, delete_flag) VALUES( " 江苏省",'+ line+''
result.append(line)
line_count += 1
print("结果是%s"%result)


return_str = "\n"
open('C:/Users/admin/Desktop/test.txt','w').write('%s'% return_str.join(result))
print("处理了文件:" +fn+"中的"+str(line_count)+"行非空白行")

实现效果如下:

 

 

###################左右两边都加内容

代码如下:

# 导入需要使用的模块
import os
import tkinter.filedialog
fn = tkinter.filedialog.askopenfilename(title='选择了一个文件',filetypes=[('文本文件','.txt'),('所有文件','.*')])
print("打开了文件:"+fn)


line_count=0
f = open(fn,'r',encoding='utf8',errors='ignore')

result = list()
for line in f.readlines():
line = line.strip()
if not len(line) or line.startswith('#'):
continue
line = 'INSERT INTO broadband_region_organization_info(province, city, country, organization_code, organization_name, `type`, create_time, update_time, delete_flag) VALUES( " 江苏省",'+ line+'"05", now(), now(), 1);'
result.append(line)
line_count += 1
print("结果是%s"%result)


return_str = "\n"
open('C:/Users/admin/Desktop/test.txt','w').write('%s'% return_str.join(result))
print("处理了文件:" +fn+"中的"+str(line_count)+"行非空白行")
实现效果如下:

 





 

posted @ 2024-01-01 09:25  往事已成昨天  阅读(119)  评论(0编辑  收藏  举报