简体   繁体   English

如何创建一个 python 程序,将字符串保存在 csv 中,但全部保存在该行的一个块中?

[英]How do create a python program that would save strings in csv but all in one block of that row?

I want this to be done我希望做到这一点
This is what I want.这就是我要的。 I have a script that gets the strings portion but whenever I try to append or write it into a CSV file it is not getting on the same block rather the strings are put in different block in a row.我有一个获取字符串部分的脚本,但是每当我尝试将其附加或写入 CSV 文件时,它都不会出现在同一个块中,而是将字符串连续放入不同的块中。 Current script does this , and I don't want this.当前脚本执行此操作,我不想要此操作。

final_string = "usafndoas asidnkas ,dioas ais asonxxk asd ascioasopasn xasix xaso asoid"

file = open("protagonist.csv", 'w+')
writer = csv.writer(file)

file.write(str(final_string))

How do I do it?我该怎么做? It is putting it into separate blocks in a row.它将它连续放入单独的块中。 But I want it on same block if possible..但如果可能的话,我希望它在同一个街区。

You need to use the csvwriter.writerow() function.您需要使用csvwriter.writerow()函数。 This takes a list of items and places each entry into its own cell in the CSV file.这需要一个项目列表,并将每个条目放入 CSV 文件中的自己的单元格中。

So to write final_string as a single value (eg A1 in Excel) you can do the following:因此, final_string写为单个值(例如 Excel 中的 A1),您可以执行以下操作:

import csv

final_string = "usafndoas asidnkas ,dioas ais asonxxk asd ascioasopasn xasix xaso asoid"

with open("protagonist.csv", 'w+', newline='') as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow([final_string])

The [] has turned final_string into a list with a single item. []final_string变成了一个包含单个项目的列表。

If you wanted to write other cells (your example shows a C in A1 ) you could change this to:如果您想编写其他单元格(您的示例显示A1中的C ),您可以将其更改为:

csv_output.writerow(["C", final_string])

This would create two cells.这将创建两个单元格。

Note: Using with here has the effect of automatically closing the file for you at the end.注意: with此处使用with具有最后自动为您关闭文件的效果。

import pandas as pd

df = pd.read_csv(r"C:\\Users\\DELL\\Desktop\\Final project\\Requirements2.csv",encoding = 'unicode_escape')

print(df)

试用此代码并为此代码提供您自己的 .csv 文件路径。

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

相关问题 如何通过移动一行使用python在CSV文件中创建新列 - How do create new column in csv file using python by shifting one row 如何创建一个循环,以便通过python将所有查询输入到一个csv中? - How do I create a loop such that I get all the queries into one csv in through python? 如何使用 Python 更新 CSV 的一列的每一行? - How do I update every row of one column of a CSV with Python? 如何循环遍历 CSV 文件中的一行 Python? - How do I loop through one row of a CSV file in Python? 如何仅删除 csv 的一行并将更改保存在同一个 csv 中? [Python] - How to delete only one row of a csv and save the changes in the same csv? [Python] Python:如何将字符串块转换为一行 - Python: How to transform a block of strings into one line 如何在文本文件中找到所有长度为 7 的字符串,然后将这些字符串保存到 Python 中的新文本文件中? - How do I find all strings of length 7 in a text file and then save those strings to a new text file in Python? 如何将CSV“单词”作为字符串传输到Python中 - How would I transfer CSV “words” into Python as strings Python创建表并另存为CSV并显示第一行内容 - Python Create Tables and save as CSV and show 1st row contents 在 python 中,您将如何为其中一列中列表的每个元素创建单独的行? - In python how would you create a separate row for each element of a list in one of the columns?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM