对行的相似列进行排序,并将行的其他项目放在排序后的行名称下方
sort row's similar columns and put row other items below the sorted row name
对行名称进行排序并在其下方打印相应的值。
文本文件包含
x 1 asd
x 2 asd
x 3 asd
x 4 asd
x 5 asd
x 5 asd
x 7 asd
b 8 axy
b 9 axc
需要输出
x
asd
asd
asd
asd
asd
asd
asd
b
axy
axc
使用 csv reader
with open("file.txt", "r") as f:
oldx, newx = '', ''
for row in csv.reader(f, delimiter=' '):
newx = row[0]
if newx != oldx:
print(newx)
oldx = newx
print(row[-1])
这是一种方法:
with open('infile.txt', 'r', encoding="utf-8") as f:
rows = [row.split() for row in f.readlines()]
print('rows:'); print(rows)
column = [rows[0][0]] + [row[-1] for row in rows]
print('column:'); print(column)
with open('outfile.txt', 'w', encoding="utf-8") as g:
for row in column:
g.write(f'{row}\n')
# check the output file:
with open('outfile.txt', 'r', encoding="utf-8") as f:
print('contents of output file:')
[print(row.strip('\n')) for row in f.readlines()]
解释:
- 从输入文件中读取所有行并将每一行拆分为一个标记列表,创建一个名为
rows
的列表列表
- 创建一个名为
column
的列表,其第一个元素是 rows
的左上角元素,其余元素来自 rows
的右列
- 将
column
的内容一次一个地写入输出文件,每行使用\n
作为行终止符
- 读取并打印输出文件以检查它包含所需的输出(注意去除最后的
\n
因为 print()
附加了它自己的 \n
)
输出:
rows:
[['x', '1', 'asd'], ['x', '2', 'asd'], ['x', '3', 'asd'], ['x', '4', 'asd'], ['x', '5', 'asd'], ['x', '5', 'asd'], ['x', '7', 'asd']]
column:
['x', 'asd', 'asd', 'asd', 'asd', 'asd', 'asd', 'asd']
contents of output file:
x
asd
asd
asd
asd
asd
asd
asd
更新:解决 OP 修改后的问题。
with open('infile.txt', 'r', encoding="utf-8") as f:
rows = [row.split() for row in f.readlines()]
print('rows:'); print(rows)
columns = []
col = []
iRow = 0
while iRow < len(rows):
if iRow == 0 or rows[iRow - 1][0] != rows[iRow][0]:
if iRow > 0:
columns.append(col)
col = [rows[iRow][0]]
col.append(rows[iRow][-1])
iRow += 1
columns.append(col)
print('columns:'); print(columns)
#column = [rows[0][0]] + [row[-1] for row in rows]
#print('column:'); print(column)
with open('outfile.txt', 'w', encoding="utf-8") as g:
isFirstCol = True
for column in columns:
if isFirstCol:
isFirstCol = False
else:
g.write(f'\n')
for row in column:
g.write(f'{row}\n')
# check the output file:
with open('outfile.txt', 'r', encoding="utf-8") as f:
print('contents of output file:')
[print(row.strip('\n')) for row in f.readlines()]
输出:
rows:
[['x', '1', 'asd'], ['x', '2', 'asd'], ['x', '3', 'asd'], ['x', '4', 'asd'], ['x', '5', 'asd'], ['x', '5', 'asd'], ['x', '7', 'asd'], ['b', '8', 'axy'], ['b', '9', 'axc']]
columns:
[['x', 'asd', 'asd', 'asd', 'asd', 'asd', 'asd', 'asd'], ['b', 'axy', 'axc']]
contents of output file:
x
asd
asd
asd
asd
asd
asd
asd
b
axy
axc
对行名称进行排序并在其下方打印相应的值。
文本文件包含
x 1 asd
x 2 asd
x 3 asd
x 4 asd
x 5 asd
x 5 asd
x 7 asd
b 8 axy
b 9 axc
需要输出
x
asd
asd
asd
asd
asd
asd
asd
b
axy
axc
使用 csv reader
with open("file.txt", "r") as f:
oldx, newx = '', ''
for row in csv.reader(f, delimiter=' '):
newx = row[0]
if newx != oldx:
print(newx)
oldx = newx
print(row[-1])
这是一种方法:
with open('infile.txt', 'r', encoding="utf-8") as f:
rows = [row.split() for row in f.readlines()]
print('rows:'); print(rows)
column = [rows[0][0]] + [row[-1] for row in rows]
print('column:'); print(column)
with open('outfile.txt', 'w', encoding="utf-8") as g:
for row in column:
g.write(f'{row}\n')
# check the output file:
with open('outfile.txt', 'r', encoding="utf-8") as f:
print('contents of output file:')
[print(row.strip('\n')) for row in f.readlines()]
解释:
- 从输入文件中读取所有行并将每一行拆分为一个标记列表,创建一个名为
rows
的列表列表
- 创建一个名为
column
的列表,其第一个元素是rows
的左上角元素,其余元素来自rows
的右列
- 将
column
的内容一次一个地写入输出文件,每行使用\n
作为行终止符 - 读取并打印输出文件以检查它包含所需的输出(注意去除最后的
\n
因为print()
附加了它自己的\n
)
输出:
rows:
[['x', '1', 'asd'], ['x', '2', 'asd'], ['x', '3', 'asd'], ['x', '4', 'asd'], ['x', '5', 'asd'], ['x', '5', 'asd'], ['x', '7', 'asd']]
column:
['x', 'asd', 'asd', 'asd', 'asd', 'asd', 'asd', 'asd']
contents of output file:
x
asd
asd
asd
asd
asd
asd
asd
更新:解决 OP 修改后的问题。
with open('infile.txt', 'r', encoding="utf-8") as f:
rows = [row.split() for row in f.readlines()]
print('rows:'); print(rows)
columns = []
col = []
iRow = 0
while iRow < len(rows):
if iRow == 0 or rows[iRow - 1][0] != rows[iRow][0]:
if iRow > 0:
columns.append(col)
col = [rows[iRow][0]]
col.append(rows[iRow][-1])
iRow += 1
columns.append(col)
print('columns:'); print(columns)
#column = [rows[0][0]] + [row[-1] for row in rows]
#print('column:'); print(column)
with open('outfile.txt', 'w', encoding="utf-8") as g:
isFirstCol = True
for column in columns:
if isFirstCol:
isFirstCol = False
else:
g.write(f'\n')
for row in column:
g.write(f'{row}\n')
# check the output file:
with open('outfile.txt', 'r', encoding="utf-8") as f:
print('contents of output file:')
[print(row.strip('\n')) for row in f.readlines()]
输出:
rows:
[['x', '1', 'asd'], ['x', '2', 'asd'], ['x', '3', 'asd'], ['x', '4', 'asd'], ['x', '5', 'asd'], ['x', '5', 'asd'], ['x', '7', 'asd'], ['b', '8', 'axy'], ['b', '9', 'axc']]
columns:
[['x', 'asd', 'asd', 'asd', 'asd', 'asd', 'asd', 'asd'], ['b', 'axy', 'axc']]
contents of output file:
x
asd
asd
asd
asd
asd
asd
asd
b
axy
axc