简体   繁体   English

csv 和 python 专家怎么用?

[英]How to use csv with python as an expert?

I'm just getting started on python programming.我刚刚开始 python 编程。

Here is an example of my CSV file:这是我的CSV文件的示例:

Name姓名 tag.标签。 description描述
Cool凉爽的 cool,fun很酷,很有趣 cool...凉爽的...
Cell细胞 Cell,phone手机 Cell...细胞...
Rang first,third第一,第三 rang...响...

The print with the CSV module gives me a list of all rows, either:带有CSV模块的 print 为我提供了所有行的列表,或者:

['cool',''cool,fun'','cool...']
['cell',''cell,phone'','cell...']

What I want to do is to printer that cool or cell, phone我想做的是打印机那酷或手机,电话


I'm also new to programming, but I think I know what you're asking.我也是编程新手,但我想我知道你在问什么。


How to use CSV module in python CSV模块在python中的使用方法

The answer for your question你的问题的答案

  • What you asked "printer that cool or cell, phone" is easy to implement, you can try below code in terminal:你问的“很酷的打印机或手机,手机”很容易实现,你可以在终端试试下面的代码:

import csv
with open('your_file_path', 'r', newline='', encoding='utf-8') as f:
    reader = csv.reader(f)
    rows = list(reader)
    print(rows[1][0])
    print(rows[2][1])

My thoughts我的想法

  • Actually, you should consider the following two points when understanding this problem:其实理解这个问题应该考虑以下两点:
  1. Content, that is, the content you want to print in your terminal, you need to first make sure that what you want is a specific row or column or a specific cell;内容,也就是你要在你的终端打印的内容,你需要先确定你要的是具体的行或列还是具体的单元格;
  2. The format, that is, the list you side or those quotation marks , these are the types of data in the file, and they must be carefully distinguished.格式,也就是你这边的列表或者那些引号,这些都是文件中的数据类型,一定要仔细区分。

In addition, it would be better for you to read some articles or materials processed about CSV module, such as the following:另外,你最好看看一些关于CSV模块加工的文章或资料,比如:

https://docs.python.org/3/library/csv.html#reader-objects https://docs.python.org/3/library/csv.html#reader-objects

https://www.geeksforgeeks.org/reading-rows-from-a-csv-file-in-python https://www.geeksforgeeks.org/reading-rows-from-a-csv-file-in-python

https://www.tutorialspoint.com/working-with-csv-files-in-python-programming https://www.tutorialspoint.com/working-with-csv-files-in-python-programming


I am also unskilled in many places, please forgive me if there are mistakes or omissions.本人也有很多地方不熟练,如有错误或遗漏,还请多多包涵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM