gpt4 book ai didi

python - 如何重新组织列中的输出结果

转载 作者:行者123 更新时间:2023-12-01 07:37:03 25 4
gpt4 key购买 nike

我的代码正在从一个文本文件中读取,该文件包含以逗号分隔的数字列表。如果存在空格和/或制表符,它将检测到它。我的代码运行良好,但现在我想让它看起来更有条理,并且更好地读取它写入的输出文件。

代码:

import os
from zipfile import ZipFile
def pain():

print("\t\t\tinput_files.zip has been unzipped")
with ZipFile('input_files.zip', 'r') as zipObj:
zipObj.extractall()
listOfFileNames = zipObj.namelist()
for fileName in listOfFileNames:
if fileName.endswith('.txt'):
zipObj.extract(fileName, 'storage')


outfile = "output2.txt" #this will be the filename that the code will write to
baconFile = open(outfile,"wt")
file_name1 = "file.txt"
print('Filename\tLine\tnumber of numbers\tstring separated by a comma\twhite space found\ttab found\tcarriage return found\n') #This prints the master column in the python shell and this is the way the code should collect the data
baconFile.write('Filename\tLine\tnumber of numbers\tstring separated by a comma\twhite space found\ttab found\tcarriage return found\n') #This prints the master column in the output file and this is the way the code should collect the data

#for filename in os.listdir(os.getcwd() + "/input_files"):
for filename in os.listdir('C:\Users\M29858\Desktop\TestPy\Version10\input_files'):
with open("input_files/" + filename, 'r') as f:
if file_name1 in filename:

output_contents(filename, f, baconFile)
baconFile.close() #closes the for loop that the code is writing to


def output_contents(filename, f, baconFile): #using open() function to open the file inside the directory
index = 0
for line in f:
#create a list of all of the numerical values in our line
content = line.split(',') #this will be used to count the amount numbers before and after comma
whitespace_found = False
tab_found = False
false_string = "False (end of file)"
carriage_found = false_string
sigfigs = ""

index += 1 #adds 1 for every line if it finds what the command wants

if " " in line: #checking for whitespace
whitespace_found = True
if "\t" in line: #checking for tabs return
tab_found = True
if '\n' in line: #checking if there is a newline after the end of each line
carriage_found = True
sigfigs = (','.join(str(len(g)) for g in re.findall(r'\d+\.?(\d+)?', line ))) #counts the sigsfigs after decimal point

print(filename + "\t{0:<4}\t{1:<17}\t{2:<27}\t{3:17}\t{4:9}\t{5:21}"
.format(index, len(content), sigfigs, str(whitespace_found), str(tab_found), str(carriage_found))) #whatever is inside the .format() is the way it the data is stored into
baconFile.write('\n')
baconFile.write( filename + "\t{0:<4}\t{1:<17}\t{2:<27}\t{3:17}\t{4:9}\t{5:21}"
.format(index, len(content), sigfigs, str(whitespace_found), str(tab_found), str(carriage_found)))



if __name__ == '__main__':
pain()```


My filename text file:
```none
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

最佳答案

您只需将所有值保存在表中即可。处理完文件后,只需迭代一个循环即可创建 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

关于python - 如何重新组织列中的输出结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56943929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com