我的代码正在读取一个文本文件,该文本文件包含一个用逗号分隔的数字列表。如果有空格和/或制表符,它将检测到它。我的代码运行良好,但是现在我想使它看起来井井有条,并且更好地读取写入的输出文件。
代码:
import re
index = 0
comma_string = ', '
outfile = "output2.txt"
wp_string = " White Space Detected"
tab_string = " tab detected"
mc_string = " Missing carriage return"
ne_string = " No Error"
baconFile = open(outfile,"wt")
num_lines = 0
with open("Version2_file.txt", 'r') as f:
for line in f: #for loop with variable line defined and using open() function to open the file inside the directory
flag = 0
carrera = ""
index = index +1 #adds 1 for every line if it finds what the command wants
print("Line {}: ".format(index))
baconFile.write("Line {}: ".format(index) + " ")
if " " in line: #checking for whitespace
carrera = wp_string + comma_string + carrera
flag = 1
a = 1
if "\t" in line: #checking for tabs return
carrera = tab_string + comma_string + carrera
flag = 1
if '\n' not in line:
carrera = mc_string + comma_string + ne_string + carrera
flag = 1
if flag == 0: #checking if no error is true by setting flag equal to zero
carrera = ne_string
print('\t'.join(str(len(g)) for g in re.findall(r'\d+\.?(\d+)?', line )))
print (carrera)
baconFile.write('\t'.join(str(len(g)) for g in re.findall(r'\d+\.?(\d+)?', line ) ))
baconFile.write(carrera + "\n")
with open("Version2_file.txt", 'r') as f:
content = f.readlines()
print('Number of Lines: {}'.format(len(content)))
for i in range(len(content)):
print('Numbers in Line {}: {}'.format(i+1, len(content[i].split(','))))
baconFile.write('Numbers in Line {}: {}'.format(i+1, len(content[i].split(','))) + '\n')
for line in format(len(content)):
baconFile.write('Number of lines: {}'.format(len(content)) + '\n')
baconFile.close()
我的文件名文本文件:
1.0, 1.023, 1.45
1.0,1.023,1.45
1
主列:文件名(制表符)行(制表符)数字(制表符)“ sigfigs”的字符串,用逗号(制表符),空格(制表符),制表符(制表符)回车
预期的输出文件:
expected:
```none
Filename Line number of numbers string of “sigfigs/decimal Places” separated by a comma white space found tab found carriage return found
filename 1 3 1,3,2 TRUE FALSE TRUE
filename 2 3 1,3,2 TRUE FALSE TRUE
filename 3 1 1 FALSE FALSE FALSE
实际:
Line 1: 1 3 2 tab detected, White Space Detected,
Line 2: 1 3 2 No Error
Line 3: 0 Missing carriage return, No Error
Numbers in Line 1: 3
Numbers in Line 2: 3
Numbers in Line 3: 1
Number of lines: 3
答案 0 :(得分:1)
您只需要将所有值保存在表中。处理完文件后,只需遍历循环即可创建一个formatted String,并将其保存在文件中。
要使您的值居中,只需使用符号^
,然后在各列之间使用空格。下面我为类似的所需输出示例:
tabled = [
['Filename', 'Line', 'number of numbers', 'string separated by a comma', 'white space found', 'tab found', 'carriage return found'],
['filename', '1', '3', '1, 3, 2', 'TRUE', 'FALSE', 'TRUE'],
['filename', '2', '3', '1, 3, 2', 'TRUE', 'FALSE', 'TRUE'],
['filename', '3', '1', '1', 'FALSE', 'FALSE', 'TRUE']
]
for row in tabled:
print("{: ^30} {: ^30} {: ^30} {: ^30} {: ^30} {: ^30} {: ^30}".format(*row))
哪个给出输出:
Filename Line number of numbers string separated by a comma white space found tab found carriage return found
filename 1 3 1, 3, 2 TRUE FALSE TRUE
filename 2 3 1, 3, 2 TRUE FALSE TRUE
filename 3 1 1 FALSE FALSE TRUE