需要一些帮助来清理一些数据。我从 cvs 文件中提取了两列,保存到 cvs 文件中并在那里进行了一些清理(删除空格等)。然后将该文件保存到制表符分隔中。当我在 Notepad ++ 中打开时,某些数据位于多行中。
我尝试了各种方法来尝试和清理,并通过执行 Notepad ++ / 编辑 / 空白操作 / EOL 到 Space 来工作,但有 1000 行需要修复。
有没有一种自动化的方法来做到这一点?选择“全部”和其中一项操作会产生不良结果,使整个文件成为我不想要的一行。数据示例:
第 1 栏第 2 栏
我希望 3. 成为:Bill Green789@@@
更好的选择是使用简单的 python 脚本按照您想要的方式排列数据。
您可以按原样使用它,也可以根据需要将其转换为函数以包含到更大的脚本中。
基本说明: 首先,创建一个新文件并将其命名为您喜欢的名称,标准命名是“name.py”接下来,在notepad++中编辑该文件并将代码粘贴到其中。设置要用作输入的 csv 文件的名称,然后设置所创建的结果文件的名称。接下来,设置要提取的列索引。最后,在cmd中运行python脚本,它将为你完成工作。
import csv
# Specify file paths and column indices
input_file = "your_input_csv_file.csv"
output_file = "your_output_file.txt"
column_indices = [0, 2] # Indices of the columns to extract (starting from 0)
# Read the CSV file
with open(input_file, 'r') as csvfile:
reader = csv.reader(csvfile)
# Write the selected columns to the new file
with open(output_file, 'w') as outfile:
for row in reader:
extracted_data = [row[i] for i in column_indices]
outfile.write(" ".join(extracted_data) + "\n")
print("File created successfully!")
这将提取您想要的列,如果您愿意,可以有两个以上的列,并将它们写入一个新文件,每行占一个新行。
多于两列的示例:
column_indices = [0, 2, 4, 6]