输入一段英文串,去除其中的逗号和句号,再切成单词列表.
时间: 2023-05-11 07:04:18 浏览: 66
Sure, here's the code to achieve that:
```python
import re
text = "This is a sample text, with some commas and periods. Let's remove them!"
text = re.sub(r'[,.]', '', text) # remove commas and periods
words = text.split() # split into word list
print(words)
```
This code uses regular expressions to remove commas and periods from the input text, and then splits the resulting string into a list of words.
相关问题
用python语言输入一个字符串,除去其中四位逗号和句号,在切成单词列表。用replace()和split()方法
可以这样实现:
```python
string = "Hello, world! This is a test, string."
string = string.replace(",", "").replace(".", "") # 去除逗号和句号
word_list = string.split() # 切成单词列表
print(word_list)
```
输出结果为:
```
['Hello', 'world', 'This', 'is', 'a', 'test', 'string']
```
如何将EXCEL中的列表按逗号分隔切割成字符串python代码
可以使用Python内置的csv模块来处理CSV文件,如果你的Excel文件是.csv格式的话,你可以直接使用这个模块。如果不是,可以使用pandas模块读取Excel文件,将数据转换为DataFrame对象后再进行处理。
下面是一个示例代码,假设你要将Excel中一列中的数据按逗号分隔切割成字符串:
```python
import pandas as pd
# 读取Excel文件,假设数据在第1个sheet中,第1列
df = pd.read_excel('your_excel_file.xlsx', sheet_name=0, usecols=[0])
# 将数据转换为字符串列表
data = df.iloc[:, 0].tolist()
# 切割每个字符串并保存结果到一个新列表中
result = []
for item in data:
result.append(item.split(','))
print(result)
```
这段代码首先使用pandas模块读取Excel文件,然后将文件中第1个sheet中的第1列数据转换为字符串列表。接着,使用split()方法将每个字符串按逗号分隔切割,并将结果保存到一个新列表中。最后,打印结果。
如果你需要将结果保存到一个新的Excel文件中,可以使用pandas模块提供的to_excel()方法。例如:
```python
# 将结果保存到一个新的Excel文件中
pd.DataFrame(result).to_excel('new_excel_file.xlsx', index=False)
```
这里将切割后的结果转换为DataFrame对象,然后使用to_excel()方法将结果保存到一个新的Excel文件中。其中,index=False表示不保存行索引。