Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将csv文件解析为文本文件_Python_C_Parsing_Csv_Find All References - Fatal编程技术网

Python 将csv文件解析为文本文件

Python 将csv文件解析为文本文件,python,c,parsing,csv,find-all-references,Python,C,Parsing,Csv,Find All References,我是一名二年级的学生。 我刚开始为我的项目学习python 我打算用如下格式解析csv文件 3520005,"Toronto (Ont.)",C ,F,2503281,2481494,F,F,0.9,1040597,979330,630.1763,3972.4,1 2466023,"Montréal (Que.)",V ,F,1620693,1583590,T,F,2.3,787060,743204,365.1303,4438.7,2 5915022,"Vancouver (B.C.)",CY

我是一名二年级的学生。 我刚开始为我的项目学习python

我打算用如下格式解析csv文件

3520005,"Toronto (Ont.)",C ,F,2503281,2481494,F,F,0.9,1040597,979330,630.1763,3972.4,1
2466023,"Montréal (Que.)",V ,F,1620693,1583590,T,F,2.3,787060,743204,365.1303,4438.7,2
5915022,"Vancouver (B.C.)",CY ,F,578041,545671,F,F,5.9,273804,253212,114.7133,5039.0,8
3519038,"Richmond Hill (Ont.)",T ,F,162704,132030,F,F,23.2,53028,51000,100.8917,1612.7,28
输入一个文本文件,如下所示

多伦多2503281 蒙特利尔1620693 温哥华578041

我正在提取第1列和第5列,并将其保存到文本文件中

这就是我目前所拥有的

import csv
file = open('raw.csv')
reader = csv.reader(file)

f = open('NicelyDone.text','w')

for line in reader:
      f.write("%s %s"%line[1],%line[5])
这对我不起作用,我能够从csv文件中提取数据,如第[1]行、第[5]行。(我可以打印出来) 但我不知道如何以我想要的格式将其写入.text文件

此外,我还必须处理第一列,如“多伦多(Ont.)”到“多伦多”。 我熟悉函数find(),我假设我可以使用“(”作为停止字符,将多伦多从多伦多(Ont.)提取出来, 但根据我的研究,我不知道如何使用它,并要求它返回字符串给我(多伦多)

我的问题是:

  • 行[1]
    的数据格式是什么?
    • 如果是字符串,为什么
      f.write()
      不起作用
    • 如果不是字符串,如何将其转换为字符串
  • 如何使用
    find()
    或其他方法将
    Toronto(Ont)
    中的单词
    Toronto
    提取为字符串形式
  • 我的想法是,我可以将这两个字符串加在一起,比如
    c=a+'''+b
    ,这将提供我想要的格式。 因此我可以使用
    f.write()
    写入文件:)

    对不起,如果我的问题听起来太简单或愚蠢

    谢谢

  • 我记不起csv了,所以我不知道它是不是字符串。你犯了什么错误?在任何情况下,假设它是一个字符串,您的行应该是:

    f.write("%s %s " % (line[1], line[5]))
    
    f.write("%s %s" % (line[1], line[5]))
    
    换句话说,您需要一组括号。此外,字符串中应该有一个尾随空格

  • 一种有点粗俗但简洁的方法是:
    行[1]。拆分(“”[0]

    这将创建一个在
    符号上拆分的列表,然后提取第一个元素

  • csv.reader
    读取的所有数据都是字符串
  • 对此有多种解决方案,但最简单的方法是在
    上拆分(
    并去掉任何空白:

    >>> a = 'Toronto (Ont.)'
    >>> b = a.split('(')
    >>> b
    Out[16]: ['Toronto ', 'Ont.)']
    >>> c = b[0]
    >>> c
    Out[18]: 'Toronto '
    >>> c.strip()
    Out[19]: 'Toronto'
    
    或者在一行中:

    >>> print 'Toronto (Ont.)'.split('(')[0].strip()
    
    另一种选择是使用正则表达式(正则表达式)

  • 代码中的具体问题在于:

    f.write("%s %s"%line[1],%line[5])
    
    使用
    %
    语法格式化字符串时,必须提供单个值或iterable。在您的情况下,这应该是:

    f.write("%s %s " % (line[1], line[5]))
    
    f.write("%s %s" % (line[1], line[5]))
    
    做同样事情的另一种方法是使用该方法

    这是一种灵活的字符串格式化方法,我建议您在中阅读


    关于你的代码,有两件事你应该考虑。

    • 请始终记住关闭文件处理程序。如果您将open(…)作为fp使用
      ,这将为您解决

      with open('myfile.txt') as ifile:
          # Do stuff
      # The file is closed here
      
    • 不要使用保留字作为变量名。
      file
      就是这样一种东西,如果将其用作其他东西(隐藏它),可能会在以后的代码中导致问题

    • 要写入数据,您可以使用:

    • 在Python2.6及更高版本中,您可以将多个
      语句组合在一个语句中:

      with open('raw.csv') as ifile, open('NicelyDone.text','w') as ofile:
          reader = csv.reader(ifile)
          writer = csv.writer(ofile)
      
    结合这些知识,您的脚本可以重写为:

    import csv
    
    with open('raw.csv') as ifile, open('NicelyDone.text', 'wb') as ofile:
        reader = csv.reader(ifile)
        writer = csv.writer(ofile, delimiter=' ')
        for row in reader:
            city, num = row[1].split('(')[0].strip(), row[5]
            writer.writerow([city, num])