python删除utf-8的csv换行
时间: 2024-01-12 22:02:47 浏览: 47
如果你想删除csv文件中的换行符,可以使用Python的csv模块来读取和写入csv文件,并使用replace()函数删除换行符。
以下是一个示例代码来删除csv文件中的换行符:
```
import csv
with open('example.csv', newline='', encoding='utf-8') as csv_file:
reader = csv.reader(csv_file)
data = [row for row in reader]
with open('example_out.csv', 'w', newline='', encoding='utf-8') as csv_file:
writer = csv.writer(csv_file)
for row in data:
writer.writerow([c.replace('\n', '') for c in row])
```
在这个例子中,我们首先打开一个csv文件并读取其内容,然后使用replace()函数删除每个单元格中的换行符。最后,我们使用csv模块中的writerow()函数将每一行写入新的csv文件中。
注意:在打开csv文件时,我们使用newline=''参数来避免在读取和写入时自动添加换行符。
相关问题
python在写入 csv中换行
在写入 csv 文件时,可以使用 `csv.writer` 中的 `lineterminator` 参数来指定换行符。默认情况下,`lineterminator` 参数为 `\r\n`,即 Windows 系统下的换行符。如果想要使用其他换行符,可以将 `lineterminator` 参数设置为相应的字符串。
例如,如果想要在写入 csv 文件时使用 Unix 系统下的换行符 `\n`,可以这样写代码:
```python
import csv
with open('data.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
writer.writerow(['Name', 'Age', 'Gender'])
writer.writerow(['Alice', '25', 'Female'])
writer.writerow(['Bob', '30', 'Male'])
```
上面的代码中,`newline=''` 参数可以避免写入 csv 文件时自动添加额外的换行符。`lineterminator='\n'` 参数指定了换行符为 Unix 系统下的换行符。
编写程序,实现以下功能。查询高校信息 兰表 成缘 没设置 描述 苏妍桥物舞积符 with open('university. csv'r 15 = Uname.readlines() #print(ls) encoding-'utf-8) as Uname: 江分雪么气市学 4122e18092,鼓与动石易远升弄:m 三有点大兴 42228109日2,新号城,龙兵为学教开 米 附件 universitv.csV中包含北京主要高校的序号、学校名称、学校标识码、主管部门、所在地、办学层次 备注等信息,以 逗字分隔行。参考提示代码,将文件内容逐行读取到列表中,根据用户输入的学校名,查询学校信息并输出。 输入格式 输入 个学校名称 输出格式 输出学校洋细信恩 《注意,行天不得有等余的换行)Python
以下是Python代码实现:
```python
with open('university.csv', 'r', encoding='utf-8') as file:
data = file.readlines()
school_name = input("请输入要查询的学校名称:")
for line in data:
info = line.split(',')
if school_name in info:
print("学校名称:", info[1])
print("学校标识码:", info[2])
print("主管部门:", info[3])
print("所在地:", info[4])
print("办学层次:", info[5])
print("备注:", info[6])
```
首先,我们打开包含高校信息的CSV文件,并将其逐行读取到一个列表中。
然后,用户输入要查询的学校名称。
接着,我们遍历列表中的每一行数据,将其按逗号分隔成一个列表,判断用户输入的学校名称是否在该列表中。
如果是,就将该行数据按要求输出。注意,输出时要使用列表的索引来获取每个字段的值。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)