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

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

Edit cell above the current one python csv

问题

  1. import csv
  2. with open('timetable.csv', newline='') as csvfile:
  3. reader = csv.reader(csvfile)
  4. for row in reader:
  5. rowId = row[0]
  6. pmp = row[1]
  7. for item in row:
  8. if item != "" and "/" in item:
  9. class_name = item
  10. editRow = int(row[0]) - 1 # Convert row[0] to an integer to perform the subtraction
  11. # Update the cell in the row above with the class_name
  12. if editRow >= 0:
  13. reader[editRow] = [rowId, pmp, class_name]

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

英文:
  1. import csv
  2. with open('timetable.csv', newline='') as csvfile:
  3. reader = csv.reader(csvfile)
  4. for row in reader:
  5. rowId = row[0]
  6. pmp = row[1]
  7. for item in row:
  8. if item != "" and "/" in item:
  9. class = item
  10. editRow = row[0] - 1
  1. -------------------------------------------------------
  2. 1 | name | ----- | class | ----- |
  3. 2 | | | class_id / class_name | |
  4. -------------------------------------------------------

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. -------------------------------------------------------
  2. 1 | name | ----- | class / class_id / class_name | ----- |
  3. 2 | | | class_id / class_name | |
  4. -------------------------------------------------------

答案1

得分: 1

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

  1. import csv
  2. with open('timetable.csv', newline='') as csvfile:
  3. reader = csv.reader(csvfile, delimiter="|")
  4. all_rows = []
  5. for row in reader:
  6. # skip lines containing only hyphens
  7. if len(row) == 1:
  8. all_rows.append(row)
  9. continue
  10. rowId = row[0]
  11. pmp = row[1]
  12. for col_num, item in enumerate(row):
  13. if all_rows and len(all_rows[-1]) > 1 and "/" in item:
  14. prev_row = all_rows[-1]
  15. prev_row[col_num] += " / " + item
  16. all_rows.append(row)

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

  1. -------------------------------------------------------
  2. 1 | name | ----- | class / class_id / class_name | ----- |
  3. 2 | | | class_id / class_name | |
  4. -------------------------------------------------------

在线尝试

  1. <details>
  2. <summary>英文:</summary>
  3. 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

  1. rowId = row[0]
  2. pmp = row[1]
  3. for col_num, item in enumerate(row):
  4. if all_rows and len(all_rows[-1]) &gt; 1 and &quot;/&quot; in item:
  5. prev_row = all_rows[-1]
  6. prev_row[col_num] += &quot; / &quot; + item
  7. all_rows.append(row)
  1. Writing this back to a file with the correct delimiter gives your desired output:
  2. ```none
  3. -------------------------------------------------------
  4. 1 | name | ----- | class / class_id / class_name | ----- |
  5. 2 | | | class_id / class_name | |
  6. -------------------------------------------------------

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:

确定