python如何另存文件,使用python添加到另一个文件以及如何保存两个文件

输入1:create external table db.emp(id int,name string)

row formatted fields terminated by ','

location 'hadfs:.../';

create external table db.emp1(id int,name string)

row formatted fields terminated by ','

location 'hadfs:.../';

输入2:create table db.emp(id int,name string)

location 'hadfs:.../';;

create table db.emp1(id int,name string)

location 'hadfs:.../';

再查询输出:create external table db.emp(id int,name string)

row formatted fields terminated by ','

location 'hadfs:.../';

create table db.emp(id int,name string)

location 'hadfs:.../';

这两个文件存储在file1.hql下create external table db.emp1(id int,name string)

row formatted fields terminated by ','

location 'hadfs:.../';

create table db.emp1(id int,name string)

location 'hadfs:.../';

这两个文件存储在file2.hql下,以此类推filenames = ['in1.txt', 'in2.txt']

with open('result.txt', 'w') as outfile:

for fname in filenames:

with open(fname) as infile:

content = infile.read().replace('\n', '')

outfile.write(content)

但是我没有得到正确的输出。请给我一个提示如何做到这一点。

使用此代码时,我得到的输出不正确

我试过密码import re

import sys

f = open ('text.txt','r')

fout = open ('hql.txt','w')

text = f.read()

fout = "hql.txt"

fout = open('hql.txt','w')

for item in re.findall(r'CREATE[^;]*;',text):

print >>fout, re.search(r'(?<=\.)\w+',item).group()+'.hql'

print >>fout,(item)

f.close()

fout.close()

此代码的o/p为:emp.hql

create external table db.emp(id int,name string)

row formatted fields terminated by ','

location 'hadfs:.../';

emp1.hql

create external table db.emp(id int,name string)

row formatted fields terminated by ','

location 'hadfs:.../';

它将向input1和input2文件生成这种输出。

现在我需要继续create external table db.emp(id int,name string)

row formatted fields terminated by ','

location 'hadfs:.../';

create table db.emp(id int,name string)

location 'hadfs:.../';

并以电磁脉冲hql等等。你知道吗

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,可以使用xlwt库来将文本文件另存为Excel文件。首先,使用open()函数读取文本文件的内容,然后使用xlwt库创建一个Excel文件并写入所读取的内容。这样,你就可以将文本文件转换成Excel文件了。 以下是一种可能的实现方法: ``` import xlwt def convert_text_to_excel(text_file, excel_file): # 打开文本文件 with open(text_file, 'r') as file: text_content = file.readlines() # 创建Excel文件 workbook = xlwt.Workbook() sheet = workbook.add_sheet('Sheet1') # 写入文本内容到Excel文件中 for row, line in enumerate(text_content): sheet.write(row, 0, line.strip()) # 保存Excel文件 workbook.save(excel_file) # 例子方法 text_file = 'input.txt' excel_file = 'output.xls' convert_text_to_excel(text_file, excel_file) ``` 在上述代码中,`convert_text_to_excel`函数接受两个参数:`text_file`是要转换的文本文件的路径,`excel_file`是要保存的Excel文件的路径。函数首先使用`open()`函数读取文本文件的内容,并将其保存在`text_content`变量中。然后,使用`xlwt.Workbook()`创建一个Excel文件对象,并使用`add_sheet()`方法在文件添加一个工作表。接下来,使用循环将文本内容逐行写入Excel文件的工作表中。最后,使用`save()`方法保存Excel文件。在代码末尾,我们提供了一个例子来演示如何使用这个函数来将文本文件转换成Excel文件。你只需要调用`convert_text_to_excel`函数并传入合适的参数即可实现将文本文件转换成Excel文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值