编辑当前单元格上面的Python CSV。

huangapple go评论65阅读模式
英文:

Edit cell above the current one python csv

问题

import csv

with open('timetable.csv', newline='') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        rowId = row[0]
        pmp = row[1]

        for item in row:
            if item != "" and "/" in item:
                class_name = item
                editRow = int(row[0]) - 1  # Convert row[0] to an integer to perform the subtraction

                # Update the cell in the row above with the class_name
                if editRow >= 0:
                    reader[editRow] = [rowId, pmp, class_name]

请注意,代码的修改可能还需要其他更多的步骤和逻辑以达到您期望的结果。上面的代码片段只是您提供的代码的一部分,处理了从第二行到第一行的数据复制。您可能需要添加更多的逻辑来处理其他方面的问题和格式化输出。

英文:
import csv
with open('timetable.csv', newline='') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        rowId = row[0]
        pmp = row[1]

        for item in row:
            if item != "" and "/" in item:
                class = item
                editRow = row[0] - 1
-------------------------------------------------------
1 | name | ----- | class | ----- |
2 |      |       | class_id / class_name |       |
-------------------------------------------------------

I have this code. I am looking for the 2nd row and I get the cell that has class_id / class_name. But the stuff I tried to go one row above to the same cell so I can paste the text from row 2 to row 1 isn't working

The desired output would be:

-------------------------------------------------------
1 | name | ----- | class / class_id / class_name | ----- |
2 |      |       | class_id / class_name |       |
-------------------------------------------------------

答案1

得分: 1

以下是您要求的翻译部分:

import csv
with open('timetable.csv', newline='') as csvfile:
    reader = csv.reader(csvfile, delimiter="|")
    all_rows = []
    for row in reader:
        # skip lines containing only hyphens 
        if len(row) == 1:
            all_rows.append(row)
            continue

        rowId = row[0]
        pmp = row[1]

        for col_num, item in enumerate(row):
            if all_rows and len(all_rows[-1]) > 1 and "/" in item:
                prev_row = all_rows[-1]
                prev_row[col_num] += " / " + item 

        all_rows.append(row)

将其写回到具有正确分隔符的文件将生成所需的输出:

-------------------------------------------------------
1 | name | ----- | class  /  class_id / class_name | ----- |
2 |      |       | class_id / class_name |       |
-------------------------------------------------------

在线尝试


<details>
<summary>英文:</summary>

You can keep track of all previously processed rows in a variable `all_rows`. Then, the previous row is accessible as `all_rows[-1]`:

import csv
with open('timetable.csv', newline='') as csvfile:
reader = csv.reader(csvfile, delimiter="|")
all_rows = []
for row in reader:
# skip lines containing only hyphens
if len(row) == 1:
all_rows.append(row)
continue

    rowId = row[0]
    pmp = row[1]

    for col_num, item in enumerate(row):
        if all_rows and len(all_rows[-1]) &gt; 1 and &quot;/&quot; in item:
            prev_row = all_rows[-1]
            prev_row[col_num] += &quot; / &quot; + item 

    all_rows.append(row)

Writing this back to a file with the correct delimiter gives your desired output:
```none
-------------------------------------------------------
1 | name | ----- | class  /  class_id / class_name | ----- |
2 |      |       | class_id / class_name |       |
-------------------------------------------------------

Try it online

huangapple
  • 本文由 发表于 2023年2月9日 00:50:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定