如何将变量打印为字符串而不是计数?

时间:2019-06-19 12:21:26

标签: python

我正在尝试搜索多个文本文件以获取“印版外观”的计数     temp_array2[0] == "play" and temp_array2[2] == "1"

我正在尝试将其导出为.csv文件,但是,当我打印“印版外观计数”时,它显示为一个计数,而不是字符串(并且我认为它必须是字符串才能导出到.csv )。

我到目前为止获得的代码如下。

import os

input_folder = 'files'  # path of folder containing the multiple text files

# create a list with file names 
data_files = [os.path.join(input_folder, file) for file in                 
os.listdir(input_folder)]

# open csv file for writing
csv = open('myoutput.csv', 'w')  
def write_to_csv(line):
    print(line)
    csv.write(line)


j=0 # initialise as 0
count_of_plate_appearances=0 # initialise as 0


for file in data_files:
    with open(file, 'r') as f:  # use context manager to open files
        for line in f:
            lines = f.readlines()
            i=0      
            while i < len(lines):
                temp_array = lines[i].rstrip().split(",")
                if temp_array[0] == "id":
                    j=0
                    count_of_plate_appearances=0
                    game_id = temp_array[1]
                    awayteam = lines[i+2].rstrip().split(",")[2]
                    hometeam = lines[i+3].rstrip().split(",")[2]
                    date = lines[i+5].rstrip().split(",")[2]
                    output_for_csv=(game_id,date,hometeam, awayteam)
                    print(output_for_csv)
                    #csv.write(','.join(output_for_csv) + '\n') #write to the csv file. This works

                    for j in range(i+46,i+120,1): #only check for plate appearances this when temp_array[0] == "id"
                        temp_array2 = lines[j].rstrip().split(",") #create new array to check for plate apperances
                        if temp_array2[0] == "play" and temp_array2[2] == "1": # plate apperance occurs when these are true
                       count_of_plate_appearances=count_of_plate_appearances+1
                    #print(count_of_plate_appearances)
                    output_for_csv2=(game_id,date,hometeam, awayteam,[count_of_plate_appearances]) #this is what I want to outpu to the CSV
                    print(output_for_csv2) 
                    #csv.write(','.join(output_for_csv2) + '\n') #the code does not run when this is uncommented

                    i=i+1

                else:
                    i=i+1

                    j=0
                    count_of_plate_appearances=0
                #quit()


csv.close() 

输出如下

('ARI201803300', '2018/03/30', 'ARI', 'COL')
('ARI201803300', '2018/03/30', 'ARI', 'COL', [35])
('ARI201803310', '2018/03/31', 'ARI', 'COL')
('ARI201803310', '2018/03/31', 'ARI', 'COL', [33])
('ARI201804020', '2018/04/02', 'ARI', 'LAN')
('ARI201804020', '2018/04/02', 'ARI', 'LAN', [32])
('ARI201804030', '2018/04/03', 'ARI', 'LAN')
('ARI201804030', '2018/04/03', 'ARI', 'LAN', [38])
('ARI201804040', '2018/04/04', 'ARI', 'LAN')

*请注意,每行有2行输出。第一个没有“印版外观数”,并且以pdf格式打印。第二行是我要打印的内容。

我认为它应该看起来像这样才能获取.csv输出。

('ARI201803300', '2018/03/30', 'ARI', 'COL', '35')
('ARI201803310', '2018/03/31', 'ARI', 'COL', '33')
('ARI201804020', '2018/04/02', 'ARI', 'LAN', '32')
('ARI201804030', '2018/04/03', 'ARI', 'LAN', '38')

关于如何执行此操作的任何建议?

1 个答案:

答案 0 :(得分:1)

将其更改为str,问题是您将其插入为list

output_for_csv2=(game_id,date,hometeam, awayteam,str(count_of_plate_appearances))