英文:
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]) > 1 and "/" in item:
prev_row = all_rows[-1]
prev_row[col_num] += " / " + 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 | |
-------------------------------------------------------
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论