Python 使用多个Delimeter读取txt文件
我有一个txt文件,有七列,没有标题。前两列之间有一个空格,其后分号用作分隔符 以下是我在意识到文件不一致之前所做的尝试:Python 使用多个Delimeter读取txt文件,python,append,Python,Append,我有一个txt文件,有七列,没有标题。前两列之间有一个空格,其后分号用作分隔符 以下是我在意识到文件不一致之前所做的尝试: min17_q1 = open('ES 03-17.Last.txt', 'r') for line in min17_q1: fields = line.split(";") Date = fields[0] Time = fields[1] Open = fields[2] High = fields[3] Low =
min17_q1 = open('ES 03-17.Last.txt', 'r')
for line in min17_q1:
fields = line.split(";")
Date = fields[0]
Time = fields[1]
Open = fields[2]
High = fields[3]
Low = fields[4]
Close = fields[5]
Volume = fields[6]
print(DateTime + Open + High + Low + Close + Volume)
如何读入此文件,然后才能操作数据和/或将相同的文件附加到此文件?要从文件中解析一行这样的内容,请在第一个空格处使用拆分,然后在分号处再次拆分:
for line in min17_q1:
Date, fields = line.split(" ", 1)
Time, Open, High, Low, Close, Volume = fields.split(';')
print(Date + Time + Open + High + Low + Close + Volume)
要连接这些文件中的多个,请使用循环:
with open('output.txt') as outfile:
# iterate over the files that need to be concatenated
for infile in ['min17_q1', 'min17_q2', 'min17_q3']:
with open(infile) as infile:
# parse each line in the file and add it to the output file
for line in infile:
line = line.replace(' ', ';', 1)
outfile.write(line)
在这种情况下,甚至不需要将每一行拆分为一个列表。我们可以用分号替换行中的第一个空格,然后写回。为此,我使用了。来解析文件中的一行,在第一个空格中使用拆分,然后在分号上再次拆分:
for line in min17_q1:
Date, fields = line.split(" ", 1)
Time, Open, High, Low, Close, Volume = fields.split(';')
print(Date + Time + Open + High + Low + Close + Volume)
要连接这些文件中的多个,请使用循环:
with open('output.txt') as outfile:
# iterate over the files that need to be concatenated
for infile in ['min17_q1', 'min17_q2', 'min17_q3']:
with open(infile) as infile:
# parse each line in the file and add it to the output file
for line in infile:
line = line.replace(' ', ';', 1)
outfile.write(line)
在这种情况下,甚至不需要将每一行拆分为一个列表。我们可以用分号替换行中的第一个空格,然后写回。为此,我使用了。这可以通过单个拆分完成:
for line in min17_q1:
Date, Time, Open, High, Low, Close, Volume = line.replace(' ',';',1).split(';')
print(Date + Time + Open + High + Low + Close + Volume)
和反向操作:
line = "%s %s;%s;%s;%s;%s;%s" % (Date, Time, Open, High, Low, Close, Volume)
这可以通过单个拆分完成:
for line in min17_q1:
Date, Time, Open, High, Low, Close, Volume = line.replace(' ',';',1).split(';')
print(Date + Time + Open + High + Low + Close + Volume)
和反向操作:
line = "%s %s;%s;%s;%s;%s;%s" % (Date, Time, Open, High, Low, Close, Volume)
我会在这个语句中使用“a”,然后将文件附加到这个语句中?min17_q1=open('ES 03-17.Last.txt','a')@user9050939我添加了一些代码,说明如何在文件中添加一行。使用第一个for循环,我已经读取了所有五个文件。我希望新文件(一个文件,每个文件附加到另一个文件)是统一的,而不是混合分隔符。在最初的for循环和print语句之后,一切都是统一的,所以这不会改变什么吗?@user9050939不确定你的意思。那会有什么变化?当然,如果您希望输出格式具有统一的分隔符,请删除
行='''.join([Date,row])
行,然后用分号连接所有内容。derp,好的,我明白您的意思了。一个完整的文件,或者五个完全相同的文件,是如何被追加的?我会在这个语句中使用“a”,然后将文件追加到这个文件中?min17_q1=open('ES 03-17.Last.txt','a')@user9050939我添加了一些代码,说明如何在文件中添加一行。使用第一个for循环,我已经读取了所有五个文件。我希望新文件(一个文件,每个文件附加到另一个文件)是统一的,而不是混合分隔符。在最初的for循环和print语句之后,一切都是统一的,所以这不会改变什么吗?@user9050939不确定你的意思。那会有什么变化?当然,如果您希望输出格式具有统一的分隔符,请删除行='''.join([Date,row])
行,然后用分号连接所有内容。derp,好的,我明白您的意思了。如何添加整个文件或五个相同的文件?假设有五个相同的文件:min17_q1、min17_q2、min17_q3、min17_q4、min18_q1。我希望它们是一个文件,在其他添加的类似文件列表之后附加一个条件,请更改samp.*和min.*假设有5个相同的文件:min17_q1、min17_q2、min17_q3、min17_q4、min18_q1。我希望它们是一个文件,在其他文件之后附加一个添加了类似文件的列表条件,请更改samp.*和min*