weixin_51029342
weixin_51029342
采纳率0%
2021-03-08 01:32

df.to_csv为什么只能显示632行的数据

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QFileDialog
import csv
import pandas as pd

class MyWindow(QtWidgets.QWidget):
    def __init__(self):
        super(MyWindow, self).__init__()
        self.myButton = QtWidgets.QPushButton(self)
        self.myButton.setObjectName("myButton")
        self.myButton.setText("Test")
        self.myButton.clicked.connect(self.msg)

    def msg(self):
        fileName, filetype = QFileDialog.getOpenFileName(self,
                                                          "选取文件",
                                                          "./",
                                                          "All Files (*);;Text Files (*.txt)")  # 设置文件扩展名过滤,注意用双分号间隔
        print(fileName)
        out = open('fileName.csv', 'w', newline='')
        csv_writer = csv.writer(out, dialect='excel')
        f = open(fileName, "r")
        for line in f.readlines():
            line = line.replace(',', '\t')  # 将每行的逗号替换成空格
            list = line.split()  # 将字符串转为列表,从而可以按单元格写入csv
            csv_writer.writerow(list)

        #writer = csv.writer(fileName, lineterminator='\n')

        df = pd.read_csv('fileName.csv')
        df.to_csv('fileName.csv', index=False)
        print(df)

if __name__ == "__main__":
    import sys


    app = QtWidgets.QApplication(sys.argv)
    myshow = MyWindow()
    myshow.show()
    sys.exit(app.exec_())
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • weixin_51029342 weixin_51029342 1月前

    点赞 评论 复制链接分享
  • weixin_51029342 weixin_51029342 1月前

    基本0基础,希望大佬能够直接改好 多次实验 知道问题就出在

    df.to_csv('fileName.csv', index=False)

    但不知道怎么改

    点赞 评论 复制链接分享
  • weixin_51029342 weixin_51029342 1月前

    df = pd.read_csv('fileName.csv')

    df.to_csv('fileName.csv', index=False)

    print(df)

    的原因是我需要删除.csv文件里面的空白行,确实可行,不过只能删除633行空白行,后面的空白行没有删除,在pycharm界面也没有显示后面的了

    点赞 评论 复制链接分享
  • weixin_51029342 weixin_51029342 1月前
    from PyQt5 import QtWidgets
    from PyQt5.QtWidgets import QFileDialog
    import csv
    import pandas as pd
    
    class MyWindow(QtWidgets.QWidget):
        def __init__(self):
            super(MyWindow, self).__init__()
            self.myButton = QtWidgets.QPushButton(self)
            self.myButton.setObjectName("myButton")
            self.myButton.setText("Test")
            self.myButton.clicked.connect(self.msg)
    
        def msg(self):
            fileName, filetype = QFileDialog.getOpenFileName(self,
                                                              "选取文件",
                                                              "./",
                                                              "All Files (*);;Text Files (*.txt)")  # 设置文件扩展名过滤,注意用双分号间隔
            print(fileName)
            out = open('fileName.csv', 'w', newline='')
            csv_writer = csv.writer(out, dialect='excel')
            f = open(fileName, "r")
            for line in f.readlines():
                line = line.replace(',', '\t')  # 将每行的逗号替换成空格
                list = line.split()  # 将字符串转为列表,从而可以按单元格写入csv
                csv_writer.writerow(list)
    
            #writer = csv.writer(fileName, lineterminator='\n')
    
            df = pd.read_csv('fileName.csv')
            df.to_csv('fileName.csv', index=False)
            print(df)
    
    
    
    if __name__ == "__main__":
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        myshow = MyWindow()
        myshow.show()
        sys.exit(app.exec_())
    点赞 评论 复制链接分享

为你推荐