文本文件中的示例数据如下:
从表1中选择*;从表2中选择*;从table3中选择*;从table4中选择*
我想阅读此文本,并在分号(;)处分成多行,并分别处理每一行。 基本上,每行代表一个选择查询,我需要针对数据库逐一查询
我尝试了下面的代码,但出现错误:AttributeError:'list'对象没有属性'split'
ListData = open(FilePath).readlines()
for line in ListData.split(';'):
data.append(line)
还尝试了下面的代码,但由于AttributeError而出错:'_io.TextIOWrapper'对象没有属性'split'
with open(SQLFilePath) as f:
for line in f.split(';'):
data.append(line)
谢谢
答案 0 :(得分:0)
尝试使用for()。例如:
text='Select * from Table1; Select * from Table2; select * from table3;Select * from table4'
for t in text.split(';'):
print(t)
答案:
Select * from Table1
Select * from Table2
select * from table3
Select * from table4
如果从文件中读取数据,则可以使用熊猫:
import pandas as pd
df=pd.read_csv(r'C:\Users\Desktop\test.txt', header = None)
t=[]
for index, row in df.itertuples():
t=t+str(row).split(';')
控制台:
print(t)
['Select * from Table1', ' Select * from Table2', ' select * from table3', 'Select * from table4', 'Select * from Table1', ' Select * from Table2', ' select * from table3', 'Select * from table4']