Python 将列重新格式化为仅前5个字符
我是Python新手,我正在努力学习这一部分。一个文本文件中大约有25列和50000多行。对于其中一列,#11(ZIP),此列包含客户的所有邮政编码值,格式为“07598-XXXX”,我只想得到前5个,因此“07598”,我需要对整个列执行此操作,但基于我当前的逻辑,我对如何编写它感到困惑。 到目前为止,我的代码能够删除包含特定字符串的行,并且我还使用了“|”分隔符将其格式化为CSV格式 注明| ZIP(#11)|第12栏|Python 将列重新格式化为仅前5个字符,python,python-3.x,Python,Python 3.x,我是Python新手,我正在努力学习这一部分。一个文本文件中大约有25列和50000多行。对于其中一列,#11(ZIP),此列包含客户的所有邮政编码值,格式为“07598-XXXX”,我只想得到前5个,因此“07598”,我需要对整个列执行此操作,但基于我当前的逻辑,我对如何编写它感到困惑。 到目前为止,我的代码能够删除包含特定字符串的行,并且我还使用了“|”分隔符将其格式化为CSV格式 注明| ZIP(#11)|第12栏| 纽约| 60169-8547 | 98 纽约| 60169-8973
纽约| 60169-8547 | 98 纽约| 60169-8973 | 58 纽约| 11219-4598 | 25 纽约| 11219-8475 | 12 纽约| 20036-4879 | 56 如何迭代ZIP列并仅显示前5个字符? 谢谢你的帮助
import csv
my_file_name = "NVG.txt"
cleaned_file = "cleanNVG.csv"
remove_words = ['INAC-EIM','-INAC','TO-INAC','TO_INAC','SHIP_TO-inac','SHIP_TOINAC']
with open(my_file_name, 'r', newline='') as infile, open(cleaned_file, 'w',newline='') as outfile:
writer = csv.writer(outfile)
for line in csv.reader(infile, delimiter='|'):
if not any(remove_word in element for element in line for remove_word in remove_words):
writer.writerow(line)
其中
zip.
是包含邮政编码的字符串。有关格式的详细信息,请参见此处:单独处理标题行,然后像您一样逐行阅读,只需将第二行行
列截断为5个字符即可
import csv
my_file_name = "NVG.txt"
cleaned_file = "cleanNVG.csv"
remove_words = ['INAC-EIM','-INAC','TO-INAC','TO_INAC','SHIP_TO-inac','SHIP_TOINAC']
with open(my_file_name, 'r', newline='') as infile, open(cleaned_file, 'w',newline='') as outfile:
writer = csv.writer(outfile)
cr = csv.reader(infile, delimiter='|')
# iterate over title line and write it as-is
writer.writerow(next(cr))
for line in cr:
if not any(remove_word in element for element in line for remove_word in remove_words):
line[1] = line[1][:5] # truncate
writer.writerow(line)
或者,您可以使用行[1]=行[1]。拆分(“-”[0]
,这将保留破折号字符左侧的所有内容
注意标题行的特殊处理:cr
是一个迭代器。我只是在for
循环执行传递处理之前手动使用它。要获取字符串中的前5个字符,请使用str[:6]
就你而言:
with open(my_file_name, 'r', newline='') as infile, open(cleaned_file, 'w',newline='') as outfile:
writer = csv.writer(outfile)
for line in csv.reader(infile, delimiter='|'):
if not any(remove_word in element for element in line for remove_word in remove_words):
line[1] = line[1][:6]
writer.writerow(line)
line[1]=line[1][:6]
会将文件中的第二列设置为其本身的前5个字符。请不要使用zip
作为字符串名称。这是一个内置函数。切片以获得子字符串也可能更好(更高效、更惯用):'11219-4598'[:5]
这很有效!谢谢问题,“writer.writerow(next(cr))”到底是如何工作的?我对这部分有点困惑。尤其是内部的cr部件。
with open(my_file_name, 'r', newline='') as infile, open(cleaned_file, 'w',newline='') as outfile:
writer = csv.writer(outfile)
for line in csv.reader(infile, delimiter='|'):
if not any(remove_word in element for element in line for remove_word in remove_words):
line[1] = line[1][:6]
writer.writerow(line)