Description The attachment is a CSV file, in which there are spaces betweendata. Please clean it by wirting a python promgram. The cleaning requirements are as follows: 1 Remove the spaces between data. That is, only separate the data with commas (,). 2 Print out after cleaning.
时间: 2024-01-31 22:04:38 浏览: 59
Sure, here's a Python program that cleans the CSV file as per the requirements:
```python
with open('file.csv', 'r') as f:
lines = f.readlines()
cleaned_lines = []
for line in lines:
cleaned_line = ','.join(line.split())
cleaned_lines.append(cleaned_line)
with open('cleaned_file.csv', 'w') as f:
f.writelines(cleaned_lines)
```
In this program, we first read the CSV file using `readlines()` method and store the lines in the `lines` list. Then, we iterate over each line and remove the spaces between data using the `split()` and `join()` methods. Finally, we write the cleaned lines to a new CSV file named `cleaned_file.csv` using the `writelines()` method.
You can replace `file.csv` with the name of your CSV file in the first `open()` method, and run this program to obtain the cleaned CSV file.
阅读全文