如何将计数变量转为字符串输出以导出至CSV文件?
Fix: Converting Numeric Plate Appearance Count to String for CSV Output
Your issue boils down to two small, easy-to-fix problems in how you're building your CSV output tuple:
- You’re wrapping the numeric count in an unnecessary list (
[count_of_plate_appearances]), which breaks','.join()since it expects string elements, not list objects. - The count itself is an integer, and
','.join()only works with iterables of strings.
Simple Direct Fix
Change the line where you define output_for_csv2 from this:
output_for_csv2=(game_id,date,hometeam, awayteam,[count_of_plate_appearances])
To this:
output_for_csv2=(game_id, date, hometeam, awayteam, str(count_of_plate_appearances))
This does exactly what you need:
- Removes the redundant list wrapper around the count value
- Converts the integer count to a string using
str()so it’s compatible with','.join()
Full Modified Code
Here’s your complete code with the fix applied, plus a couple of minor quality-of-life improvements (like using a context manager for the CSV file to guarantee it closes properly):
import os input_folder = 'files' # path of folder containing the multiple text files data_files = [os.path.join(input_folder, file) for file in os.listdir(input_folder)] # Use context manager for CSV file to avoid accidental unclosed files with open('myoutput.csv', 'w') as csv_file: j = 0 # initialise as 0 count_of_plate_appearances = 0 # initialise as 0 for file in data_files: with open(file, 'r') as f: lines = f.readlines() # Read all lines once instead of inside the loop 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) # Check for plate appearances for j in range(i+46, i+120): temp_array2 = lines[j].rstrip().split(",") if temp_array2[0] == "play" and temp_array2[2] == "1": count_of_plate_appearances += 1 # Fixed line here output_for_csv2 = (game_id, date, hometeam, awayteam, str(count_of_plate_appearances)) print(output_for_csv2) csv_file.write(','.join(output_for_csv2) + '\n') # Now this runs without errors i += 1 else: i += 1 j = 0 count_of_plate_appearances = 0
Bonus: Use Python’s Built-in csv Module
For more robust CSV handling (like automatically escaping commas in fields, proper quoting, and handling edge cases), consider using Python’s native csv module instead of manual string joining. It even handles converting integers to strings for you:
import os import csv input_folder = 'files' data_files = [os.path.join(input_folder, file) for file in os.listdir(input_folder)] with open('myoutput.csv', 'w', newline='') as csv_file: writer = csv.writer(csv_file) # Optional: Add a header row for clarity writer.writerow(["game_id", "date", "hometeam", "awayteam", "plate_appearances"]) count_of_plate_appearances = 0 for file in data_files: with open(file, 'r') as f: lines = f.readlines() i = 0 while i < len(lines): temp_array = lines[i].rstrip().split(",") if temp_array[0] == "id": 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] for j in range(i+46, i+120): temp_array2 = lines[j].rstrip().split(",") if temp_array2[0] == "play" and temp_array2[2] == "1": count_of_plate_appearances += 1 # Write row directly with the integer count (csv.writer handles conversion) writer.writerow([game_id, date, hometeam, awayteam, count_of_plate_appearances]) i += 1 else: i += 1
内容的提问来源于stack exchange,提问作者Lauren
相关产品推荐
相关产品推荐

