简体   繁体   English

获取 csv 列表的“坐标”

[英]Getting 'coordinates' of a csv list

I have a CSV file with values that can change and the file can be appended using a module I made.我有一个 CSV 文件,其中的值可以更改,并且可以使用我制作的模块附加该文件。 I want to make a module that can search whether a value is contained in the file and its location in the file.我想制作一个模块,可以搜索文件中是否包含某个值及其在文件中的位置。 What i have right now is:我现在拥有的是:

import csv
def GET_ROW_COUNT():
    with open('battle_royale.csv', 'r') as source:
        battleRoyaleData = csv.reader(source, delimiter=',')
        row_count = sum(1 for row in battleRoyaleData)
    return row_count
def DISPLAY_PLAYERS():
    with open('battle_royale.csv', 'r') as source:
        battleRoyaleData = csv.reader(source, delimiter=',')
        for row in battleRoyaleData:
            print(row)
def WRITE_PLAYER(avatarName, name):
    csv_list = []
    rowCount = GET_ROW_COUNT()
    with open('battle_royale.csv', 'r') as source:
        battleRoyaleData = csv.reader(source, delimiter=',')
        for row in battleRoyaleData:
            csv_list.append(row)
        csv_list.append([f"'{avatarName}'", f"'{name}'", f"'{rowCount}'"])
    with open('battle_royale.csv', 'w', newline='') as csvfile:
        newWrite = csv.writer(csvfile, delimiter=',')
        newWrite.writerows(csv_list)

I'm thinking I would use我想我会用

data = []
    with open('battle_royale.csv', 'r') as source:
        battleRoyaleData = csv.reader(source, delimiter=',')
        for row in battleRoyaleData:
            row = [t for t in row] 
            data.append(row)

and then include like然后包括像

if (value) in data:
    coordinates = #some way to get the position of the value in the list from index

----Edit---- How can I get the position of a value and know whether the value exists in a list? ----编辑---- 如何获取值的位置并知道该值是否存在于列表中?

As I can see, You trying to implement CRUD operations for csv files.如我所见,您试图对 csv 文件实施 CRUD 操作。

  • Create创建
  • Read
  • Update更新
  • Delete删除

For this purpose, you can use sqlite3 database and simple SQL queries.为此,您可以使用sqlite3数据库和简单的 SQL 查询。
It won't be harder than your idea with csv.使用 csv 不会比您的想法更难。 https://docs.python.org/3/library/sqlite3.html https://docs.python.org/3/library/sqlite3.html
It also has a nice client to interact with the data.它还有一个很好的客户端来与数据交互。 https://sqlitebrowser.org/ https://sqlitebrowser.org/

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

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